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

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

我如何在Razor中做到这一点?


当前回答

我的解决办法很愚蠢,但很有效。 在静态文件中定义一个全局常量:

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

其他回答

对我来说,下面的代码运行得非常好。

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

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

在.NET Core中,你可以使用环境标记帮助器来代替检查预处理器变量:

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

这适用于我的。net Core 3.0白色标签项目:

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

我需要在<script>标记中工作的类似内容,并发现以下内容对于DOM中的条件标记或条件脚本都工作得很好。

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

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

我知道这不是对问题的直接回答,但我非常确定调试配置是实际在本地执行的事实的必然结果,您总是可以使用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">
}