基于条件编译符号启用 MVCBuildViews

Rya*_*n B 6 msbuild asp.net-mvc visual-studio

是否可以向您的构建添加条件步骤以检查自定义条件编译符号并启用 MVCBuildViews。我已经找到了一种基于构建配置的方法来做到这一点

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <MvcBuildViews>true</MvcBuildViews>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)

但不确定如何访问编译符号。

计划是在控制MVCBuildViews的项目设置>构建>条件编译符号下添加一个符号

V2S*_*eam 0

假设您使用的是 C#,您需要将编译器配置为在构建视图时使用 Eg.TEST符号,为此您需要使用Web.config以下内容覆盖其配置:

<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" //Here you need to define your symbol
          warningLevel="1" />
      </compilers>
    </system.codedom>
Run Code Online (Sandbox Code Playgroud)

如果您直接在 Web.config 中应用它,它将起作用,但会定义例如。TEST每次。因此,您真正应该做的是使用Web.config transformations,以便该符号仅应用于正确的构建配置。

您可以通过执行以下步骤来更新使用以前版本的 MVC 创建的项目,以包括视图的构建时验证:

1)在文本编辑器中打开项目文件。

2)在最顶层元素下添加以下元素:<MvcBuildViews>true</MvcBuildViews>

3)在项目文件的末尾,取消注释该<Target Name="AfterBuild">元素并修改它以匹配以下内容:

<Target Name="AfterBuild" Condition="'$(MvcBuildViews)'=='true'">
    <AspNetCompiler VirtualPath="temp" PhysicalPath="$(ProjectDir)\..\$(ProjectName)" />
</Target>
Run Code Online (Sandbox Code Playgroud)