#DEBUG Preprocessor statements in ASPX page

前端 未结 8 1019
滥情空心
滥情空心 2020-12-29 22:09

I\'m trying to use a preprocessor directive in an ASPX page, but the page doesn\'t recognize it. Is this just something I can\'t do?

Background: I\'m trying to inclu

相关标签:
8条回答
  • 2020-12-29 22:38

    I tried your code, and it worked fine for me. I enabled or disabled DEBUG from the system.web/compilation section in web.config, running as a web site (didn't test as a web application; might be different...).

    To see what that code does, put an intentional syntax error in the page, and try to run it with debug mode enabled. The compiler will generate a link on the error page that will allow you to view the source.

    Hint: the pre-processor directives are inserted into the output.

    Line 218:     #if DEBUG 
    Line 219:              
    Line 220:              #line default
    Line 221:              #line hidden
    Line 222:              @__w.Write("\r\n<script type=\"text/javascript\" src=\"resources/jquery-1.3.2.js\" />\r\n");
    Line 223:              
    Line 224:              #line 14 "F:\Test\test.aspx"
    Line 225:     #else 
    Line 226:              
    Line 227:              #line default
    Line 228:              #line hidden
    Line 229:              @__w.Write("\r\n<script type=\"text/javascript\" src=\"resources/jquery-1.3.2.min.js\" />\r\n");
    Line 230:              
    Line 231:              #line 16 "F:\Test\test.aspx"
    Line 232:     #endif
    

    Of course, there are other (better) ways to do what you're after...

    0 讨论(0)
  • 2020-12-29 22:41

    To me the most elegant solution would be to simply define a field in code-behind with preprocessor directives and then check for its value from the aspx page.

    In code-behind:

    public partial class WebClient : System.Web.UI.Page
    {        
    #if DEBUG 
        public bool DebugMode = true;
    #else
        public bool DebugMode = false;
    #endif
    }
    

    Aspx page:

    <%if(this.DebugMode){%>
        <script type="text/javascript" src="resources/jquery-1.3.2.js" />
    <%}%>
    <%else{%>
        <script type="text/javascript" src="resources/jquery-1.3.2.min.js" />
    <%}%>
    
    0 讨论(0)
  • 2020-12-29 22:43

    I tried to achieve this many ways over the years with out any success. After playing with asp.net mvc recently I got this idea. There is another possibility to keep it all in aspx page using invalid MIME types. In asp.net MVC, templates are loaded with invalid MIME types, so that the browser will not parse them. I followed the same pattern here; Wola! Success... You need one static function that gives you easy access to debug constraint.

    <script src='<%= Page.ResolveUrl("~/Scripts/jquery-1.5.2.min.js") %>' 
        **type='<%=if(Ops.IsRelease(),"text/javascript","HideMin")%>'**></script> 
    

    Ops.IsRelease() is Public Static Function which returns the debug constaraint

        Public Shared Function IsRelease() As Boolean
            Dim [release] = False
    #If Not debug Then
            [release]=True
    #End If
            Return [release]
        End Function
    
    0 讨论(0)
  • 2020-12-29 22:49

    Here is my solution:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (System.Diagnostics.Debugger.IsAttached)
        {
            ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "JQueryScript", "resources/jquery-1.3.2.js");
        }
        else
        {
            ScriptManager.RegisterClientScriptInclude(this, this.GetType(), "JQueryScript", "resources/jquery-1.3.2.min.js");
        }
    }
    
    0 讨论(0)
  • 2020-12-29 22:50

    Interesting difference here - using #if DEBUG in the aspx page pulls from the tag in the web.config, but when you use it in the code-behind, it pulls DEBUG from the constant from the build configuration in the project file. So they're actually accessing two different settings.

    Thus, as far as I can tell, this isn't actually possible.

    0 讨论(0)
  • 2020-12-29 22:55

    I had the same problem, I tried to solve it in both asp.mvc application and webform application. The #if debug always return true, it never takes into account the web.config compilation setting. I solved it in similar way to Syam advice posted in this post, but instead of static function I use astatcic variable as described here: http://www.psworld.pl/Programming/DebugDirective

    0 讨论(0)
提交回复
热议问题