如何使用web.Release.config转换进行URL重写?

Mat*_*amp 45 asp.net asp.net-mvc web-config-transform

我有一个web.config重写规则指定将所有流量移动到https.该规则有效,但我在调试时不需要SSL.我已经完成了一堆web.release.config转换工作,因此我决定在那里放一个重写规则.问题是重写规则没有像其他设置那样进行转换.这是web.config设置:

<system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules runAllManagedModulesForAllRequests="true"/>

    <rewrite></rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

以下是正在进行的转型:

  <system.webServer>
<rewrite>
  <rules>
    <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
      <match url="(.*)"/>
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$"/>
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther"/>
    </rule>
  </rules>
</rewrite></system.webServer>
Run Code Online (Sandbox Code Playgroud)

如果我只是将重写规则复制到web.config它可以正常工作.有没有人有任何想法为什么web.Release.config转换不仅适用于此部分?

Lob*_*ity 44

只有xdt在需要转换的元素上放置适当的属性时才会发生转换.尝试xdt:Transform在发布配置中添加属性:

<system.webServer xdt:Transform="Replace">
    <!-- the rest of your element goes here -->
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

这将告诉转换引擎需要将整个system.webServer元素Web.config替换为Web.Release.config.

转换引擎将默默地忽略任何没有xdt属性的元素.

MSDN的强制性链接.

  • 对于文件更复杂的人来说,请注意.你可以将`xdt:Transform`元素放在`<rewrite>`标签上并将其更改为Insert,如下所示:`<rewrite xdt:Transform ="Insert">`这将保留你的`<system中的任何其他标签. webServer>`section. (39认同)
  • 我讨厌web.config转换.谁设计了这个. (4认同)
  • 实际上``web.config`变换非常流畅......有些人只是被困在过去...... (4认同)
  • 什么是好的架构?我有'http://schemas.microsoft.com/XML-Document-Transform',VS 2015告诉我xdt:Transform未在<rule>上声明 (2认同)

cit*_*nas 33

另一种方法是设置一个重写条件,如果你在localhost上则否定:

<conditions>
    <add input="{HTTP_HOST}" pattern="localhost" negate="true"/>
</conditions>
Run Code Online (Sandbox Code Playgroud)

  • 这比变换要好得多,谢谢! (2认同)
  • 很棒的解决方案.值得注意的是,如果您使用Chrome - Chrome会缓存重定向 - 因此您可能需要清除缓存:http://superuser.com/questions/304589/how-can-i-make-chrome-stop-caching-redirects (2认同)

Ray*_*der 9

<system.webServer>
    <rewrite>
        <rules xdt:Transform="Replace">
            <clear />
            <rule name="Redirect HTTP to HTTPS" stopProcessing="true">
              <match url="(.*)" />
              <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{HTTP_HOST}" pattern="localhost(:\d+)?" negate="true" />
                <add input="{HTTP_HOST}" pattern="127\.0\.0\.1(:\d+)?" negate="true" />
                <add input="{HTTPS}" pattern="OFF" />
              </conditions>
              <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="SeeOther" />
            </rule>
        </rules>          
    </rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

  • 如果你的web.config中没有重写元素,你也可以使用<rules xdt:Transform ="Insert">. (3认同)