Bad*_*jer 24 c# asp.net c-preprocessor
我正在尝试在ASPX页面中使用预处理程序指令,但页面无法识别它.这只是我不能做的事情吗?
背景:我试图在DEBUG模式中包含完整版本的jQuery(for,well,debugging =)),以及用于发布的缩小版本.我尝试过这个,但我对ASPX <%语法并不十分熟悉.我只是从根本上误解了这种语法的作用吗?
<% #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 %>
Run Code Online (Sandbox Code Playgroud)
Bad*_*jer 16
这里有趣的区别 - 在aspx页面中使用#if DEBUG来自web.config中的标记,但是当你在代码隐藏中使用它时,它会从项目文件中的构建配置中的常量中提取DEBUG.所以他们实际上访问了两个不同的设置.
因此,据我所知,这实际上是不可能的.
更好的方法可能是使用服务器端代码来包含脚本.我会用类似的东西
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
}
Run Code Online (Sandbox Code Playgroud)
对我来说,最优雅的解决方案是使用预处理器指令在代码后面简单地定义一个字段,然后从aspx页面检查其值。
在后面的代码中:
public partial class WebClient : System.Web.UI.Page
{
#if DEBUG
public bool DebugMode = true;
#else
public bool DebugMode = false;
#endif
}
Run Code Online (Sandbox Code Playgroud)
Aspx页面:
<%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" />
<%}%>
Run Code Online (Sandbox Code Playgroud)