[英]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.