Asp.net 在发布配置中插入重写规则

Asp.net 在发布配置中插入重写规则,asp.net,xslt,web-config-transform,xdt-transform,Asp.net,Xslt,Web Config Transform,Xdt Transform,嗨,我想为“重定向到HTTPS”插入重写规则,但只在我的发布配置上 这就是重写规则的外观 <system.webServer> <rewrite> <rules> <rule name="Redirect to HTTPS"> <match url="(.*)" /> <conditions>

嗨,我想为“重定向到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>


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

您可以查看web.config转换:

创建和编码转换文件的步骤

  • 如果您创建的生成配置不存在转换文件 要指定的设置,请在解决方案资源管理器中,右键单击 Web.config文件,然后单击添加配置转换
  • 打开要使用的生成配置的转换文件
  • 编辑转换文件以指定在使用该生成配置进行部署时应对已部署的Web.config文件所做的更改。默认转换文件包含注释,说明如何对一些常见转换进行编码

  • 只需在需要插入web.config发布版本的元素上添加
    xdt:Transform=“Insert”
    属性。例如,如果初始web.config根本不包含
    元素,则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>
    
    <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>