asp.net-mvc Razor 视图引擎,如何进入预处理器(#if debug)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4696175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Razor view engine, how to enter preprocessor(#if debug)
提问by mamu
I am writing my first razor page today, can't figure out how to enter #if debug #else #endif
我今天正在写我的第一个剃须刀页面,不知道如何进入 #if debug #else #endif
How can i enter preprocessor in razor?
如何在剃刀中输入预处理器?
回答by Shawn Wildermuth
I just created an extension method:
我刚刚创建了一个扩展方法:
public static bool IsDebug(this HtmlHelper htmlHelper)
{
#if DEBUG
return true;
#else
return false;
#endif
}
Then used it in my views like so:
然后在我的视图中使用它,如下所示:
<section id="sidebar">
@Html.Partial("_Connect")
@if (!Html.IsDebug())
{
@Html.Partial("_Ads")
}
<hr />
@RenderSection("Sidebar", required: false)
</section>
Since the helper is compiled with the DEBUG/RELEASE symbol, it works.
由于帮助程序是使用 DEBUG/RELEASE 符号编译的,因此它可以工作。
回答by Jordan Gray
This is built in to HttpContext
:
@if (HttpContext.Current.IsDebuggingEnabled)
{
// Means that debug="true" in Web.config
}
IMO, this makes more sense than conditional compilation for views and comes in handy for some testing scenarios. (See Tony Wall's commentbelow.)
IMO,这比视图的条件编译更有意义,并且在某些测试场景中派上用场。(请参阅下面的托尼·沃尔(Tony Wall)评论。)
Side note: NullReferenceException
for HttpContext.Current
旁注:NullReferenceException
对于HttpContext.Current
Alex Angas mentionedthat they get a NullReferenceException
with this solution, and a few people have upvoted indicating that this may not be an isolated event.
Alex Angas 提到他们得到了NullReferenceException
这个解决方案,一些人表示这可能不是一个孤立的事件。
My best guess: HttpContext.Current
is stored in CallContext
, meaning it is only accessible by the thread that handles the incoming HTTP request. If your views are being rendered on a different thread (perhaps some solutions for precompiled views?) you would get a null
value for HttpContext.Current
.
我最好的猜测:HttpContext.Current
存储在 中CallContext
,这意味着它只能由处理传入 HTTP 请求的线程访问。如果你的意见被在不同的线程(也许是预编译的观点一些解决方案?)渲染,你会得到一个null
为值HttpContext.Current
。
If you get this error, please let me know in the comments and mention if you are using precompiled views or anything special set up that could result in your views being partially rendered/executed on another thread!
如果您收到此错误,请在评论中告诉我,并说明您是否使用预编译视图或任何可能导致您的视图在另一个线程上部分呈现/执行的特殊设置!
回答by Buildstarted
C# and ASP.NET MVC: Using #if directive in a view
C# 和 ASP.NET MVC:在视图中使用 #if 指令
Actually that answer has the right answer. You're going to have to pass whether or not you're in debug mode via the Model. (or ViewBag) since all views are compiled in debug mode.
其实这个答案有正确的答案。无论您是否处于调试模式,您都必须通过模型传递。(或 ViewBag),因为所有视图都是在调试模式下编译的。
回答by Sbu
I know this is not a direct answer to the question but as I'm pretty sure debug configuration is corollary to the fact that you are actually executing locally, you can always use the Request.IsLocal
property as a debug like test. Thus :
我知道这不是问题的直接答案,但我很确定调试配置是您实际在本地执行的必然结果,您始终可以将该Request.IsLocal
属性用作类似测试的调试。因此 :
@if (Request.IsLocal)
{
<link rel="stylesheet" type="text/css" href="~/css/compiled/complete.css">
}
else
{
<link rel="stylesheet" type="text/css" href="~/css/compiled/complete.min.css">
}
回答by tedebus
My solution is very stupid, but it works. Define a global constant somewhere in a static file:
我的解决方案非常愚蠢,但它有效。在静态文件中的某处定义一个全局常量:
public static class AppConstants
{
#if DEBUG
public const bool IS_DEBUG = true;
#else
public const bool IS_DEBUG = false;
#endif
}
Then use it with Razor in HTML:
然后在 HTML 中将它与 Razor 一起使用:
@if (AppConstants.IS_DEBUG)
{
<h3>Debug mode</h3>
}
else
{
<h3>Release mode</h3>
}
回答by Yannick Richard
By default MVC views are not compiled so #IF DEBUG can't work in a view. If you want to compile view in order to access IF DEBUG config, you need to :
默认情况下,不会编译 MVC 视图,因此 #IF DEBUG 无法在视图中工作。如果要编译视图以访问 IF DEBUG 配置,则需要:
- Right click on your project in Visual Studio
- Unload project
- Edit project
- 在 Visual Studio 中右键单击您的项目
- 卸载项目
- 编辑项目
change the following attribute from false to true
将以下属性从 false 更改为 true
<MvcBuildViews>true</MvcBuildViews>
reload your project and then views are going to be compiled.
重新加载您的项目,然后将编译视图。
The only other work around would be to have a function in your code behind
唯一的其他解决方法是在您的代码中添加一个函数
public static Boolean DEBUG(this System.Web.Mvc.WebViewPage page)
{
var value = false;
#if(DEBUG)
value=true;
#endif
return value;
}
and then call it from view :
然后从视图中调用它:
if(DEBUG())
{
//debug code here
}
else
{
//release code here
}
回答by Matheus Fernandes Amorim
For me, the code below has worked very well.
对我来说,下面的代码效果很好。
When the application is Debuggingmy buttons appear, when is Release, they don't.
当应用程序正在调试我的按钮时会出现,当是Release 时,它们不会出现。
@if (this.Context.IsDebuggingEnabled)
{
<button type="button" class="btn btn-warning">Fill file</button>
<button type="button" class="btn btn-info">Export file</button>
}
回答by Perry Armstrong
This works for me in a white label project .net core 3.0
这在白标项目 .net core 3.0 中对我有用
@{
#if CORPA
}
<button type="button" class="btn btn-warning">A Button</button>
@{
#else
}
<p>Nothing to see here</p>
@{
#endif
}
回答by beleester
In .NET Core, you can do the following instead of checking the preprocessor variables:
在 .NET Core 中,您可以执行以下操作而不是检查预处理器变量:
<environment include="Development">
<!--Debug code here-->
</environment>