【问题标题】:How do I make URL rewrite work with web.Release.config transform?如何使 URL 重写与 web.Release.config 转换一起工作?
【发布时间】:2011-10-23 15:51:18
【问题描述】:

我指定了一个 web.config 重写规则来将所有流量转移到 https。该规则有效,但我不希望在调试时需要 SSL。我已经在发布时完成了一堆 web.release.config 转换,所以我决定在其中放置一个重写规则。问题是重写规则没有像其他设置一样被转换。这是 web.config 设置:

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

    <rewrite></rewrite>
</system.webServer>

这是正在完成的转换:

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

如果我只是将重写规则复制到 web.config 它工作正常。有没有人知道为什么 web.Release.config 转换仅适用于本节?

【问题讨论】:

  • 我也很想知道这是怎么回事。转换的行为似乎不像它应该的那样。

标签: asp.net asp.net-mvc web-config-transform


【解决方案1】:

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

<system.webServer xdt:Transform="Replace">
    <!-- the rest of your element goes here -->
</system.webServer>

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

转换引擎将静默忽略任何不具有xdt 属性的元素。

MSDN 的必填链接。

【讨论】:

  • 文件更复杂的人的额外说明。您可以将xdt:Transform 元素放在&lt;rewrite&gt; 标记上并将其更改为插入,如下所示:&lt;rewrite xdt:Transform="Insert"&gt; 这将保留您的&lt;system.webServer&gt; 部分中的任何其他标记。
  • 我讨厌 web.config 转换。这是谁设计的。
  • 其实web.config的转换很灵巧……有些人只是停留在过去……
  • 使用什么好的架构?我有'schemas.microsoft.com/XML-Document-Transform',VS 2015 告诉我 xdt:Transform 没有在 上声明
  • @Stephane 看起来模式喜欢父级 &lt;system.webServer&gt; 中的 Transform,但不希望它在下面的 &lt;rewrite&gt;&lt;rules&gt; 中。但其他人报告它仍然有效。
【解决方案2】:

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

<conditions>
    <add input="{HTTP_HOST}" pattern="localhost" negate="true"/>
</conditions>

【讨论】:

  • 很棒的解决方案。值得注意的是,如果您使用的是 Chrome - Chrome 会缓存重定向 - 因此您可能需要清除缓存:superuser.com/questions/304589/…
【解决方案3】:
<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>

【讨论】:

  • 如果您的 web.config 中没有重写元素,您也可以使用
【解决方案4】:

在这里总结其他答案,我们发现很明显:“替换”只会替换一个节点,而不是“插入”它(感谢 DigitalD 的正确轨道)。我们的其余转换文件使用替换,因此我们在基本 web.config 中选择了一个空标签(被转换的那个)。

<system.webServer>
...other tags here that do not get transformed...
<rewrite />
</system.webServer>

理想情况下会有插入或替换(或删除和插入)的“覆盖”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 2012-10-16
    • 2011-12-02
    • 2012-01-06
    • 2016-05-24
    • 1970-01-01
    • 2017-04-25
    相关资源
    最近更新 更多