[英]Replacing IIS rewrite rules in web.config transform
我有一些 IIS 重写规则,我希望这些规则因环境而异。 开发重写规则在 web.config 文件中,然后在 web.test.config 文件的末尾我有:
<appSettings>
...Some app settings tranforms here
</appSettings>
<system.webserver>
<rewrite xdt:Transform="Replace">
<rules>
... rules here
</rules>
</rewrite>
</system.webserver>
</configuration>
当我部署到测试时,我的应用程序设置正在转换,但 IIS 重写规则没有。 我希望整个<rewrite>
部分可以简单地替换为转换文件中的部分(根据http://msdn.microsoft.com/en-us/library/dd465326.aspx ),但没有任何变化。
我也尝试将xdt:Transform="Replace" xdt:Locator="Match(name)">
放在各个规则上:
<rule name="Test rule" stopProcessing="true" xdt:Transform="Replace" xdt:Locator="Match(name)">
但这同样没有区别。
甚至可以替换 web.config 中的重写规则,如果可以,我错过了什么?
由于我的主 web.config 中没有任何重写规则,因此替换转换不起作用。 我成功地使用了插入转换,如下所示:
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="CanonicalHostNameRule1">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.mysite\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mysite.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
这里有很多带有示例的答案,这是一件好事,但我认为缺少一些细节。 我已经在我的网站上写过这个,这里的关键点是在要为相应环境添加的根标记层次结构中添加xdt:Transform="Insert"
。
默认情况下,您有 Web.config 文件,但也有 Web.Debug.config 和 Web.Release.config,如下图所示:
假设您想在应用程序版本中添加从 http 到 https 的重定向。 然后编辑 Web.Release.config 并添加以下行:
<?xml version="1.0"?>
.....
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
......
</rules>
</rewrite>
</system.webServer>
</configuration>
因此,下次您发布项目时,带有 rewrite 的标记及其子内容将添加到 web.config 文件中。
要在发布之前查看,请右键单击 Web.Release.config,然后单击预览转换。
您将看到初始版本和发布版本之间的区别。
参考:
免责声明:本指南的链接是指我的个人网站。
起初,在创建发布配置时,重写部分对我来说很奇怪,错误和部分根本没有显示。 这就是我解决它的方法。
Microsoft (R) 构建引擎版本 12.0.31101.0
Microsoft .NET Framework,版本 4.0.30319.0
编辑在搞乱这个之后,我意识到在没有重写插件的服务器上使用重写标签会使网络服务器返回错误。 我想要服务器和本地开发机器上的不同配置,所以修复是:
未转换的 web.config 只需要一个 <system.webServer> 标记,并且在 web.config.release 中用于基本规范主机名规则
<configuration>
<system.webServer>
<rewrite xdt:Transform="Insert">
<rules>
<rule name="CanonicalHostNameRule" xdt:Transform="Insert">
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" pattern="^www\.host\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.host.com/{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
该操作根本不需要名称,但重写标签需要 xdt:Transform="Insert"
显然,如果您也希望在本地计算机上使用它,则需要进行更新。
可以转换 system.webServer 的重写部分。 我最初遇到了同样的问题,并意识到我无意中将重写节点错误地放置在 system.web 下。 虽然根据您提供的有限片段,这看起来不像您的问题,但我仍然怀疑您的问题与转换文件中的节点放置有关。
这是我的 Web.Debug.config 的样子(这个版本在调试版本上编写了正确的 Web.config):
<?xml version="1.0"?>
<!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<!--
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an atrribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
-->
<system.web>
<!--
In the example below, the "Replace" transform will replace the entire
<customErrors> section of your web.config file.
Note that because there is only one customErrors section under the
<system.web> node, there is no need to use the "xdt:Locator" attribute.
<customErrors defaultRedirect="GenericError.htm"
mode="RemoteOnly" xdt:Transform="Replace">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
-->
</system.web>
<system.webServer>
<rewrite xdt:Transform="Replace">
<rules>
<clear/>
<rule name="Canonical Hostname">
<!-- Note that I have stripped out the actual content of my rules for the purposes of posting here... -->
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
我使用的一个技巧是给动作一个名字
然后在我的转换中添加xdt:Transform="SetAttributes" xdt:Locator="Match(name)"
如下所示
<system.webServer>
<rewrite>
<rules>
<rule name="RedirecttoWWW" enabled="true" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action name="AddWWW" type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
</rule>
</rules>
</rewrite>
上面的例子是在所有请求中添加www
- - - -更新 - - -
只是向操作添加名称的更新将无法正常工作,因此我将代码更新如下
<system.webServer>
<rule name="RedirecttoWWW" enabled="true" xdt:Transform="RemoveAll" xdt:Locator="Match(name)" >
</rule>
<rule name="RedirecttoWWW" enabled="true" xdt:Transform="InsertIfMissing" xdt:Locator="Match(name)" >
<match url="(.*)" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^www\.([.a-zA-Z0-9]+)$" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
</rule>
</rules>
</rewrite>
</system.webServer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.