3

嗨,我想为“重定向到 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 中实现这一点?

4

2 回答 2

7

只需xdt:Transform="Insert"在需要插入到 web.config 的发布版本的元素上添加属性。例如,如果您的初始 web.config 根本不包含<rewrite>元素,则 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 已经包含一些其他规则,那么您只需要在元素级别添加xdt:Transform="Insert"属性:<rule>

<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> 
于 2016-05-14T08:56:16.497 回答
0

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

创建和编码转换文件

  1. 如果要为其指定设置的构建配置不存在转换文件,请在解决方案资源管理器中右键单击 Web.config 文件,然后单击添加配置转换
  2. 打开您要使用的构建配置的转换文件。
  3. 编辑转换文件以指定在使用该生成配置进行部署时应对已部署的 Web.config 文件进行的更改。默认转换文件包含说明如何编写一些常见转换的注释。
于 2016-02-24T11:03:32.507 回答