我正在使用 MinifyJS.tt,它是一个 T4 模板,可以自动缩小我所有的 JS 文件。 在我的 aspx 文件中,我引用了所有的 javascript 文件。
现在,我想添加一个条件(可能是编译器指令),以便在调试应用程序时使用原始 JS 文件,并在不调试的情况下仅运行应用程序时使用缩小后的 JS 文件。 我尝试在 aspx 页面中使用 #if,但这似乎不起作用。
我们可以在 aspx 页面中使用预处理器指令吗? 有没有其他方法可以实现我的目标?
您可以使用以下内容:
if (!HttpContext.Current.IsDebuggingEnabled)
Script = OptimizeScript(Script);
进一步.....除了进一步讨论这个话题还有一些评论。
来自 Wilco Bauwer 他评论说这个属性封装了 web.config 设置并且没有考虑页面级调试,如果你想......
bool isDebuggingEnabled = Assembly.GetExecutingAssembly().IsDefined(typeof(DebuggableAttribute));
....这是实现它的孩子!
Peter Bromberg(C# MVP)提供了另一种解决方案,使用 Global.asax.cs 文件和在 Application_Start 事件中设置的静态全局应用程序标志。
public static bool IsDebugMode = false;
protected void Application_Start(object sender, EventArgs e)
{
if (System.Diagnostics.Debugger.IsAttached) IsDebugMode = true;
取自Steves博客