【问题标题】:Inserting Rewrite rule in release config在发布配置中插入重写规则
【发布时间】:2016-06-06 14:51:23
【问题描述】:

您好,我想为“重定向到 HTTPS”插入一个重写规则,但仅限于我的发布配置

这就是重写规则的样子

<system.webServer>
    <rewrite>
        <rules>
            <rule name="Redirect to HTTPS">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                    <add input="{URL}" pattern="/$" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                </conditions>
                <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

如何仅在我的 release.config 中实现这一点?

【问题讨论】:

    标签: asp.net xslt web-config-transform xdt-transform


    【解决方案1】:

    只需在需要插入到 web.config 的发布版本的元素上添加 xdt:Transform="Insert" 属性。例如,如果您的初始 web.config 根本不包含 &lt;rewrite&gt; 元素,那么 release.config 应该如下:

    <system.webServer>
        <rewrite xdt:Transform="Insert">
          <rules>
            <rule name="Redirect to HTTPS">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                <add input="{URL}" pattern="/$" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              </conditions>
              <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer>
    

    否则,如果初始的 web.config 已经包含一些其他规则,那么您只需在 &lt;rule&gt; 元素级别添加 xdt:Transform="Insert" 属性即可:

    <system.webServer>
        <rewrite>
          <rules>
            <rule name="Redirect to HTTPS" xdt:Transform="Insert">
              <match url="(.*)" />
              <conditions>
                <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                <add input="{URL}" pattern="/$" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
              </conditions>
              <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="SeeOther" />
            </rule>
          </rules>
        </rewrite>
    </system.webServer> 
    

    【讨论】:

      【解决方案2】:

      您可以查看 web.config 转换: https://msdn.microsoft.com/library/dd465318(v=vs.100).aspx

      创建和编码转换文件

      1. 如果不存在用于构建配置的转换文件 要指定设置,在解决方案资源管理器中,右键单击 Web.config 文件,然后单击添加配置转换
      2. 打开您要使用的构建配置的转换文件。
      3. 编辑转换文件以指定在使用该构建配置进行部署时应对已部署的 Web.config 文件进行的更改。默认的转换文件包括展示如何编写一些常见转换的 cmets。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-05-24
        • 1970-01-01
        • 1970-01-01
        • 2020-02-27
        • 1970-01-01
        • 2021-09-24
        • 2018-04-26
        相关资源
        最近更新 更多