开发者

#DEBUG Preprocessor statements in ASPX page

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 include the full version of jQuery in DEBUG mode (for, well, debugging =) ), and the minified version for release. I tried this, but I'm not terribly familiar with the ASPX <% syntax. Am I just f开发者_如何学Cundamentally misunderstanding what this syntax does?

<% #if DEBUG %>
<script type="text/javascript" src="resources/jquery-1.3.2.js" />
<% #else %>
<script type="text/javascript" src="resources/jquery-1.3.2.min.js" />
<% #endif %>


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.


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" />
<%}%>


A better approach may be to use server side code to include the script. I'd use something like

protected void Page_Load(object sender, EventArgs e)
{
#if DEBUG    
    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");
    #endif
}


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...


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


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


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");
    }
}


I don't think you can have preprocessor directives in the aspx unfortunately.

A simpler way to go is to simply have a property in your code-behind that feeds in the jQuery URL, then you can set preprocessor directives to declare it. Or if you'd prefer to keep the URL in the code-infront you could use a Literal control and toggle their visibility in the code-behind based on the processor directives.

For example:

code-infront:

<asp:literal id="litJQuery" EnableViewState="false" runat="Server">
<script type="text/javascript" src="resources/jquery-1.3.2.js" />
</asp:literal>
<asp:literal id="litJQueryDebug" EnableViewState="false" Visible="false" runat="Server">
<script type="text/javascript" src="resources/jquery-1.3.2.min.js" />
</asp:literal>

code-behind, in the Page_Load method:

#if DEBUG
litJQueryDebug.Visible=true;
litJQuery.Visible=false;
#endif
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜