簡體   English   中英

ASP.NET MVC:是否有語法可訪問web.config中的項目條件編譯符號

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

ASP.NET MVC:在web.config是否存在訪問項目條件編譯符號的語法?

例如,我可以在每個區域使用不同的數據庫,或者確定是否需要HTTPS等。

在2012年,類似的問題似乎對此並不樂觀: ASP.NET-有條件的Web.config

.config文件不是構建的一部分,因此我不確定您希望通過執行config轉換無法完成的條件來實現什么。 XML是標記-它本身不包含任何行為 為此,您需要某種轉換引擎。

<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>

然而, 條件在MSBuild的支持。 這意味着您可以通過手動編輯將條件放入.csproj (或.vbproj )文件中。 有關更多信息,請參見此答案

<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>

在上面的示例中,如果條件編譯字段包含MVC2 ,則包括第一組引用,如果條件編譯字段包含MVC3 ,則包括第二組引用。 請注意,這些符號以分號分隔。

<DefineConstants>MVC2;NET35;</DefineConstants>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM