Razor 中的预处理器指令

问题描述 投票:0回答:11

我今天正在编写我的第一个 Razor 页面,但不知道如何进入

#if debug
...
#else
...
#endif

我怎样才能在 Razor 中做到这一点?

c# asp.net-mvc razor preprocessor
11个回答
409
投票

我刚刚创建了一个扩展方法:

public static bool IsDebug(this HtmlHelper htmlHelper)
{
#if DEBUG
      return true;
#else
      return false;
#endif
}

然后在我的观点中使用它,如下所示:

<section id="sidebar">
     @Html.Partial("_Connect")
     @if (!Html.IsDebug())
     { 
         @Html.Partial("_Ads")
     }
     <hr />
     @RenderSection("Sidebar", required: false)
</section>

由于助手是使用 DEBUG/RELEASE 符号编译的,因此它可以工作。


325
投票

这是内置于

HttpContext

@if (HttpContext.Current.IsDebuggingEnabled)
{
    // Means that debug="true" in Web.config
}

IMO,这比视图的条件编译更有意义,并且对于某些测试场景非常有用。 (参见下面托尼·沃尔的评论。)


旁注:
NullReferenceException
代表
HttpContext.Current

Alex Angas 提到,他们通过这个解决方案得到了

NullReferenceException
,并且有一些人投票表示这可能不是一个孤立的事件。

我的最佳猜测:

HttpContext.Current
存储在
CallContext
中,这意味着它只能由处理传入HTTP请求的线程访问。如果您的视图在不同的线程上渲染(也许有一些预编译视图的解决方案?),您将获得
null
HttpContext.Current
值。

如果您收到此错误,请在评论中告诉我,并提及您是否正在使用预编译视图或任何可能导致您的视图在另一个线程上部分渲染/执行的特殊设置!


24
投票

C# 和 ASP.NET MVC:在视图中使用 #if 指令

其实这个答案有正确答案。无论您是否通过模型处于调试模式,您都必须通过此测试。 (或 ViewBag),因为所有视图都是在调试模式下编译的。


23
投票

在 .NET Core 中,您可以使用 环境标签助手 而不是检查预处理器变量:

<environment include="Development">
    <!--Debug code here-->
</environment>

20
投票

我的解决方案非常愚蠢,但它有效。 在静态文件中的某处定义全局常量:

public static class AppConstants
{
#if DEBUG
        public const bool IS_DEBUG = true;
#else
        public const bool IS_DEBUG = false;
#endif
}

然后在 HTML 中与 Razor 一起使用:

@if (AppConstants.IS_DEBUG)
{
    <h3>Debug mode</h3>
}
else
{
    <h3>Release mode</h3>
}

15
投票

我知道这不是问题的直接答案,但我很确定调试配置是您实际在本地执行这一事实的必然结果,因此您始终可以使用

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

12
投票

这在 .NET Core 3.0 白标项目中对我有用:

@{
#if CORPA
}
    <button type="button" class="btn btn-warning">A Button</button>
@{
#else
}
    <p>Nothing to see here</p>
@{
#endif
}

5
投票

默认情况下,MVC 视图不会被编译,因此 #IF DEBUG 无法在视图中工作。如果您想编译视图以访问 IF DEBUG 配置,您需要:

  1. 在 Visual Studio 中右键单击您的项目
  2. 卸载项目
  3. 编辑项目

将以下属性从 false 更改为 true

<MvcBuildViews>true</MvcBuildViews>

重新加载您的项目,然后将编译视图。

唯一的其他解决办法是在你的代码后面有一个函数

public static Boolean DEBUG(this System.Web.Mvc.WebViewPage page)
{
   var value = false;
   #if(DEBUG)
       value=true;
   #endif
   return value;
}

然后从视图中调用它:

if(DEBUG())
{
  //debug code here
}
else
{
  //release code here
}

5
投票

对我来说,下面的代码非常有效。

当应用程序调试时,我的按钮出现,当发布时,它们不会出现。

@if (this.Context.IsDebuggingEnabled)
{
    <button type="button" class="btn btn-warning">Fill file</button>
    <button type="button" class="btn btn-info">Export file</button>
} 

3
投票

我也需要在

<script>
标签中使用类似的东西,并发现以下内容对于 DOM 中的条件标记或条件脚本都适用。

@{
#if NOEXTAUTH
{
    @:<!-- A single line of code -->

    <text>
        <!--
        A multi-line block    
        -->
    </text>
}
#endif
}

0
投票

据我所知,最简单的解决方案是:

@{
#if DEBUG
}
<div id="blazor-error-ui">
   <environment include="Staging,Production">
    An error has occurred. This application may no longer respond until reloaded.
   </environment>
   <environment include="Development">
    An unhandled exception has occurred. See browser dev tools for details.
   </environment>
   <a href="" class="reload">Reload</a>
   <a class="dismiss">🗙</a>
</div>
@{
#endif
}
© www.soinside.com 2019 - 2024. All rights reserved.