C# 和 ASP.NET MVC:在视图中使用 #if 指令
我正在使用一个名为“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我
最近发现您可以简单地在视图中测试:,这样您就无需检查应用程序其他部分中的符号。
I
recentlydiscovered that you can simply test:in Views, which saves you checking symbols in other parts of your app.
更好、更通用的解决方案是使用扩展方法,因此所有视图都可以访问它:
然后您可以在任何视图中使用它,如下所示(razor 语法):
A better, more generic solution is to use an extension method, so all views have access to it:
You can then use it like follows in any view (razor syntax):
在您的模型中:
在您看来:
In your model:
In your view:
您可以使用 ViewBag 而不是 viewmodel (但类似 viewmodel 的方法更好):
Controller :
View :
用法 :
Also ,请确保 NIKITA_DEBUG 变量存在于项目的构建选项卡中:
You can use ViewBag instead of viewmodel (but viewmodel-like approach is better) :
Controller :
View :
Usage :
Also, be sure, that NIKITA_DEBUG variable exist in build tab of your project :
对我来说,下面的代码非常有效。当应用程序正在调试时,我的按钮出现,当发布时,则不出现。
For me, the code below has worked very well. When the application is Debugging my buttons appear, when is Release, don't.
您可以像这样使用 Debugger.IsAttached :
You can use Debugger.IsAttached like this:
以下是条件编译器指令的 Razor 语法。当在 VS 配置文件或 web.config 中设置 DEBUG 变量时,它会加载 jquery 的开发人员版本。否则加载最小版本。
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.