如何修复 web.config 中重写部分的无效配置错误
How do I fix an invalid configuration error for a rewrite section in web.config
我的 web.config 有以下 rewrite
元素在我的机器上被注释掉了,但是当部署到我们的 QA 服务器时,它没有被注释掉。我的机器和QA的区别是QA有SSL证书而我的机器没有。
<system.webServer>
<!--<rewrite>
<rules>
<clear/>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false"/>
</rule>
</rules>
</rewrite>-->
当这个没有被注释掉时,我得到一个 500.19 错误,配置无效?我的问题是,如何让这个 rewrite
在我的机器上运行?我想安装证书是解决方案的一部分,但我想知道,如果有的话,我还需要在 IIS 等中做什么才能使项目达到 运行 而无需注释掉此配置元素?
利用改造web.config
参考:Transform web.config 和其他资源以更好地了解如何应用转换。
在发布配置中包含 <rewrite>
元素
例如
Web.Release.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<clear/>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>
<configuration>
它会在发布站点(发布模式)时将 <rewrite>
元素作为构建过程的一部分插入到配置文件中
可以根据需要在本地计算机上使用默认版本,因为该元素不是主配置文件的一部分,因为它只在发布版本中需要。
Web.config
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<!-- ... -->
</connectionStrings>
<system.web>
<!-- ... -->
</system.web>
<!-- populated as desired, excluding the <rewrite> element -->
</configuration>
单一配置方式
在这种情况下,在您的本地计算机上,您必须安装 URL 重写模块 ,它 不 包含在除了 安装证书 之外,默认使用 IIS(使用网络平台安装程序)。这样IIS就可以使用配置里面的rewrite
块了。
基于环境的动态配置
在这种情况下,您让编译器根据环境使用相关配置。正如@Nkosi 提到的,每个环境都有两个版本的配置文件(嵌套):
- Web.Debug.config
- Web.Release.config
我的 web.config 有以下 rewrite
元素在我的机器上被注释掉了,但是当部署到我们的 QA 服务器时,它没有被注释掉。我的机器和QA的区别是QA有SSL证书而我的机器没有。
<system.webServer>
<!--<rewrite>
<rules>
<clear/>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false"/>
</rule>
</rules>
</rewrite>-->
当这个没有被注释掉时,我得到一个 500.19 错误,配置无效?我的问题是,如何让这个 rewrite
在我的机器上运行?我想安装证书是解决方案的一部分,但我想知道,如果有的话,我还需要在 IIS 等中做什么才能使项目达到 运行 而无需注释掉此配置元素?
利用改造web.config
参考:Transform web.config 和其他资源以更好地了解如何应用转换。
在发布配置中包含 <rewrite>
元素
例如
Web.Release.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<clear/>
<rule name="Redirect to https" stopProcessing="true">
<match url=".*"/>
<conditions>
<add input="{HTTPS}" pattern="off" ignoreCase="true"/>
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false"/>
</rule>
</rules>
</rewrite>
</system.webServer>
<configuration>
它会在发布站点(发布模式)时将 <rewrite>
元素作为构建过程的一部分插入到配置文件中
可以根据需要在本地计算机上使用默认版本,因为该元素不是主配置文件的一部分,因为它只在发布版本中需要。
Web.config
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<!-- ... -->
</connectionStrings>
<system.web>
<!-- ... -->
</system.web>
<!-- populated as desired, excluding the <rewrite> element -->
</configuration>
单一配置方式
在这种情况下,在您的本地计算机上,您必须安装 URL 重写模块 ,它 不 包含在除了 安装证书 之外,默认使用 IIS(使用网络平台安装程序)。这样IIS就可以使用配置里面的rewrite
块了。
基于环境的动态配置
在这种情况下,您让编译器根据环境使用相关配置。正如@Nkosi 提到的,每个环境都有两个版本的配置文件(嵌套):
- Web.Debug.config
- Web.Release.config