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

发布于 2024-09-03 17:48:57 字数 741 浏览 5 评论 0原文

我正在使用一个名为“RELEASE”的条件编译符号,我在 Visual Studio 中的项目属性中指出了该符号。我希望在定义 RELEASE 符号时将一些特定的 CSS 应用于元素,并且我试图从视图中执行此操作,但它似乎不起作用。

我的视图代码如下所示(为了演示目的而缩短了一点):

<% #if (RELEASE) %>
    <div class="releaseBanner">Banner text here</div>
<% #else %>
    <div class="debugBanner">Banner text here</div>
<% #endif %>

使用此代码并使用 RELEASE 符号集,“else”代码正在运行,并且我得到了带有 debugBanner 类的 div。所以看起来好像没有定义RELEASE。值得注意的是,.cs 文件中的实际 C# 代码正在识别 RELEASE 并运行正确的代码。这只是给我带来问题的观点。

有人对此有任何见解吗?任何帮助将不胜感激。谢谢。

说明:我应该提到此视图已经是部分视图,我将简单地将其呈现在我需要的页面中。这是因为这些横幅将出现在某些页面上,而不是其他页面上。因此,即使通过以下方式将其渲染为部分视图:

Html.RenderPartial("BannerView");

它也不起作用。

I've got a conditional compilation symbol I'm using called "RELEASE", that I indicated in my project's properties in Visual Studio. I want some particular CSS to be applied to elements when the RELEASE symbol is defined, and I was trying to do that from the view, but it doesn't seem to be working.

My view code looks like this (shortened a bit for demo purposes):

<% #if (RELEASE) %>
    <div class="releaseBanner">Banner text here</div>
<% #else %>
    <div class="debugBanner">Banner text here</div>
<% #endif %>

With this code, and with the RELEASE symbol set, the 'else' code is running and I'm getting a div with the debugBanner class. So it doesn't seem to think that RELEASE is defined. It's worth noting that my actual C# code in .cs files is recognizing RELEASE and runs the correct code. It's only the view that is giving me the problem.

Does anyone have any insight into this? Any help would be appreciated. Thanks.

Clarification: I should have mentioned that this view is already a partial view, and I'll simply render it in pages where I need it. That's because these banners will be on certain pages and not others. So even when rendering it as a partial view via:

Html.RenderPartial("BannerView");

it's not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

情深如许 2024-09-10 17:48:57

最近发现您可以简单地

HttpContext.Current.IsDebuggingEnabled

在视图中测试:,这样您就无需检查应用程序其他部分中的符号。

I recently discovered that you can simply test:

HttpContext.Current.IsDebuggingEnabled

in Views, which saves you checking symbols in other parts of your app.

草莓酥 2024-09-10 17:48:57

更好、更通用的解决方案是使用扩展方法,因此所有视图都可以访问它:

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

然后您可以在任何视图中使用它,如下所示(razor 语法):

@if(Html.IsReleaseBuild())
...

A better, more generic solution is to use an extension method, so all views have access to it:

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

You can then use it like follows in any view (razor syntax):

@if(Html.IsReleaseBuild())
...
你的笑 2024-09-10 17:48:57

在您的模型中:

bool isRelease = false;

<% #if (RELEASE) %>
    isRelease = true;
<% #endif %>

在您看来:

<% if (Model.isRelease) { %>
    <div class="releaseBanner">Banner text here</div>
<% } else { %>
    <div class="debugBanner">Banner text here</div>
<% } %>

In your model:

bool isRelease = false;

<% #if (RELEASE) %>
    isRelease = true;
<% #endif %>

In your view:

<% if (Model.isRelease) { %>
    <div class="releaseBanner">Banner text here</div>
<% } else { %>
    <div class="debugBanner">Banner text here</div>
<% } %>
忆伤 2024-09-10 17:48:57
@if (HttpContext.Current.IsDebuggingEnabled)
{
    // Debug mode enabled. Your code here. Texts enclosed with <text> tag
}
@if (HttpContext.Current.IsDebuggingEnabled)
{
    // Debug mode enabled. Your code here. Texts enclosed with <text> tag
}
原野 2024-09-10 17:48:57

您可以使用 ViewBag 而不是 viewmodel (但类似 viewmodel 的方法更好):

Controller :

controller code

View :

@{
   bool hideYoutubeVideos = ViewBag.hideYoutubeVideos ?? false;     
}

用法 :

@if (!hideYoutubeVideos)
{
     <span>hello youtube</span>
}

Also ,请确保 NIKITA_DEBUG 变量存在于项目的构建选项卡中:

build tab

You can use ViewBag instead of viewmodel (but viewmodel-like approach is better) :

Controller :

controller code

View :

@{
   bool hideYoutubeVideos = ViewBag.hideYoutubeVideos ?? false;     
}

Usage :

@if (!hideYoutubeVideos)
{
     <span>hello youtube</span>
}

Also, be sure, that NIKITA_DEBUG variable exist in build tab of your project :

build tab

呆° 2024-09-10 17:48:57

对我来说,下面的代码非常有效。当应用程序正在调试时,我的按钮出现,当发布时,则不出现。

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

For me, the code below has worked very well. When the application is Debugging my buttons appear, when is Release, don't.

@if (this.Context.IsDebuggingEnabled)
{
    <button type="button" class="btn btn-warning">Fill file</button>
    <button type="button" class="btn btn-info">Export file</button>
} 
萌︼了一个春 2024-09-10 17:48:57

您可以像这样使用 Debugger.IsAttached

@using System.Diagnostics

@{
    string gridID = $"the-grid-7";

    if (Debugger.IsAttached)
        gridID = gridID + new Random().Next(1, 1000).ToString();

    var loadUrl = ViewBag.LoadUrl;
}

You can use Debugger.IsAttached like this:

@using System.Diagnostics

@{
    string gridID = 
quot;the-grid-7";

    if (Debugger.IsAttached)
        gridID = gridID + new Random().Next(1, 1000).ToString();

    var loadUrl = ViewBag.LoadUrl;
}
夜清冷一曲。 2024-09-10 17:48:57

以下是条件编译器指令的 Razor 语法。当在 VS 配置文件或 web.config 中设置 DEBUG 变量时,它会加载 jquery 的开发人员版本。否则加载最小版本。

    @{
#if (DEBUG)
    }
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.js"></script>
    @{    
#else
    }
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
       <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>
    @{
#endif
    }

Below is the Razor syntax for conditional compiler directives. It loads the developer version of jquery when DEBUG variable is set in VS profile or web.config. Otherwise the min version is loaded.

    @{
#if (DEBUG)
    }
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.js"></script>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.js"></script>
    @{    
#else
    }
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js"></script>
       <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.12/jquery-ui.min.js"></script>
    @{
#endif
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文