在MVC视图中使用条件编译符号 [英] Using conditional compilation symbols in MVC views

查看:297
本文介绍了在MVC视图中使用条件编译符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目的属性,我有以下几点:

In "Properties" of my project I have the following:

我要检查是否测试符号存在,只有到那时,做一些事情。
所以我做了什么,你在下面的图片,并在它的工作原理类看。然而,这并不在视图中工作。

I want to check if TEST symbol exists, and only then, do some things. So I did what you see in the picture below and in the class it works. However this does not work in the views.

在此块中的文字是灰色的,即使测试定义!

The text in this block is gray even if TEST is defined!

怎么能因为如果测试定义它的工作?

How can I cause it work if TEST is defined?

推荐答案

问题是相关的事实是,当你运行你的应用程序,以便在测试视图只编译你定义的编译器不再适用,因为它没有它的知识的象征。

The problem is related to the fact that views are only compiled when you run your application so the TEST symbol that you defined is no longer applied by the compiler because it has no knowledge of it.

假设你正在使用C#,你需要配置编译器建设的意见时使用的 TEST 象征,为此,你需要的override其配置在<​​/A> 使用的Web.config 如下:

Assuming that you are using C# you need to configure the compiler to use the TEST symbol when building the views and for this you need to override its configuration in Web.config using the following:

<system.codedom>
  <compilers>
    <compiler
      language="c#;cs;csharp"
      extension=".cs"
      type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
      compilerOptions="/define:TEST"
      warningLevel="1" />
  </compilers>
</system.codedom>

最重要的部分是你定义的 compilerOptions =/定义:TEST。您需要调整您的特定需求配置的其余部分,例如.NET 2.0或.NET 4.0之间切换。

The important part is that you define compilerOptions="/define:TEST". The rest of the configuration you need to adapt to your specific needs, for example switch between .NET 2.0 or .NET 4.0.

如果您直接在的Web.config将此将工作,但将定义测试每次。所以,你应该做的是利用 Web.config文件转换中,这样的符号只适用于正确的构建配置。

If you apply this directly in the Web.config it will work but will define TEST every time. So what you should really do is use Web.config transformations so that the symbol is only applied for the correct build configurations.

这篇关于在MVC视图中使用条件编译符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆