在ASP.Net MVC站点中有条件地包含基于调试或生产的部分视图

Joh*_*n S 10 c# asp.net-mvc razor

我有一个局部视图,只包括基本的HTML,没有剃刀代码或模型.我用这个来为页面布局设置一些"指南".

当站点在调试模式下运行时,仅包含此部分的正确/最简单方法是什么?

我知道在我编译的代码中,我可以在我的C#代码中使用指令来包含部分.剃刀有类似的东西吗?

jav*_*iry 13

您可以使用HttpContext.Current.IsDebuggingEnabled哪个检查web.configs'调试设置:

@if(HttpContext.Current.IsDebuggingEnabled) {
    //Do something here.
}
Run Code Online (Sandbox Code Playgroud)

使用扩展帮助方法:

public static Boolean DEBUG(this System.Web.Mvc.WebViewPage page) {
// use this sequence of returns to make the snippet ReSharper friendly. 
#if DEBUG
        return true;
#else
        return false;
#endif
}
Run Code Online (Sandbox Code Playgroud)

和用法:

@if(DEBUG()) {
    //debug code here
} else {
    //release code here
}
Run Code Online (Sandbox Code Playgroud)