2012-08-08 38 views
37

我有一些IIS重写规则,我希望根据环境的不同而不同。发展重写规则是在web.config文件中,然后在web.test.config文件我的末尾:替换web.config变换中的IIS重写规则

<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中的重写规则,如果是这样,我错过了什么?

回答

0

可以转换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> 
38

,因为我没有在任何重写规则我主web.config,Replace转换不起作用。我成功地使用了插入变换,如下:

<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> 
0

我使用一个小技巧是给动作的名称
然后在我的变换只需添加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添加到所有请求

------- UPDATE -----

只是被通缉,所以我的代码更新如下

<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> 
6

创建释放CONFIGS,错误和部分不显示时,重写部分古怪的工作对我来说是第一次的更新添加名称的行动将无法正常工作在所有。这是我解决它的方式。

微软(R)构建引擎版本12.0.31101.0

的Microsoft .NET Framework版本4.0.30319.0

编辑摆弄这之后我意识到,具有重写标签在没有重写插件的服务器上,Web服务器返回错误。我wan't服务器和本地发展的机器不同的配置,因此解决方法是:

将未转化的web.config中只需要一个标签,并在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:转换=“插入”

显然,如果你想在你的本地计算机藏汉,那就需要更新替代。