Using conditional compilation symbols in MVC views

て烟熏妆下的殇ゞ 提交于 2019-12-04 03:09:56

问题


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?


回答1:


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.

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>

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.

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.




回答2:


Rather than specify the compiler flag in web.config as per the accepted answer (which also requires specifying the compiler version in web.config, which is a non-standard location) I went with the following:

Add a method to a base class shared by my models

public bool IsDebugBuild 
{ 
    get
    {
        #if DEBUG
        return true;
        #else
        return false;
        #endif
    }
}

Use that method in my views

if (mm.IsDebugBuild) {
    <div class="debug">
    // Do Stuff
    </div>
}



回答3:


The symbol you set is only used during compilation. It does not exist otherwise. So, your web project's DLL does not have that symbol at all. Therefore, when the View is compiled. the symbol isn't there, and it won't work as you are expecting.




回答4:


I don't think its possible to use conditional symbols in a view as Andrew Barber has already said.

But you could use conditional symbols in the model:

public class ViewModel
{
//...whatever else you need to define

    private bool test;

    public bool Test
    {
        get
        {
            return test;
        }
    }

    public ViewModel()
    {
        #if (TEST)
            test = true;
        #endif
    }
}

And then check the value in the view:

@{
    if (Model.Test)
    {
        <p>debug statements here</p>
    }
}


来源:https://stackoverflow.com/questions/8324947/using-conditional-compilation-symbols-in-mvc-views

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!