JosephDoggie
JosephDoggie

Reputation: 1594

ASP.NET MVC: Is there a syntax to access a project conditional compile symbol in web.config

ASP.NET MVC: is there a syntax to access a project conditional compile symbol in web.config?

e.g. I could use different databases on a per region basis, or determine whether to require HTTPS, etc.

A similar question in 2012 did not appear to be optimistic about this: ASP.NET - Conditional Web.config

Upvotes: 1

Views: 128

Answers (1)

NightOwl888
NightOwl888

Reputation: 56869

The .config files are not part of the build, so I am uncertain what you hope to achieve by doing conditions there that can't be done with a config transform. XML is markup - it doesn't contain any behavior by itself. For that, you need a transform engine of some kind.

<system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
        In the example below, the "Replace" transform will replace the entire 
        <customErrors> section of your web.config file.
        Note that because there is only one customErrors section under the 
        <system.web> node, there is no need to use the "xdt:Locator" attribute.

    <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
    </customErrors>
-->
    <sessionState xdt:Transform="Remove"/>
    <trace xdt:Transform="Remove"/>
</system.web>

However, conditions are supported in MSBuild. This means that you can put conditions inside of your .csproj (or .vbproj) file by hand editing it. See this answer for more info.

<ItemGroup Condition=" $(DefineConstants.Contains('MVC2')) ">
    <Reference Include="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
</ItemGroup>
<ItemGroup Condition=" $(DefineConstants.Contains('MVC3')) ">
    <!-- Due to the windows update MS14-059, we need this hack to ensure we can build MVC3 both on machines that have the update and those that don't -->
    <Reference Condition=" Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
    <Reference Condition=" !Exists('$(windir)\Microsoft.NET\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll') " Include="System.Web.Mvc, Version=3.0.0.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
        <Private>True</Private>
        <HintPath>..\packages\Microsoft.AspNet.Mvc.3.0.20105.1\lib\net40\System.Web.Mvc.dll</HintPath>
    </Reference>
    <Reference Include="System.Web.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
        <Private>True</Private>
        <HintPath>..\packages\Microsoft.AspNet.Razor.1.0.20105.408\lib\net40\System.Web.Razor.dll</HintPath>
    </Reference>
    <Reference Include="System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
        <Private>True</Private>
        <HintPath>..\packages\Microsoft.AspNet.WebPages.1.0.20105.408\lib\net40\System.Web.WebPages.Razor.dll</HintPath>
    </Reference>
</ItemGroup>

In the above example, if the conditional compilation field contains MVC2 the first set of references are included, if it contains MVC3, the second set of references are included. Note that the symbols are semi-colon delimited.

<DefineConstants>MVC2;NET35;</DefineConstants>

Upvotes: 1

Related Questions