替换 web.config 转换中的 IIS 重写规则

51

我有一些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中替换重写规则是否可能,如果可能的话,我错过了什么?

5个回答

57

由于我的主要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>

你能指导我如何在 Parameters.xml 文件中实现这个吗? - Bat_Programmer
@Bat_Programmer 我不熟悉 Parameters.xml 文件。建议您提出一个新问题,并详细说明您想要实现的内容。 - StuartQ

12

这里有很多带有示例的答案,这是一件好事,但我认为缺少了一些细节。我在我的网站中写过关于此的内容,关键点在于在您想要添加到相应环境的根标记层次结构中添加xdt:Transform ="Insert"

默认情况下,您有Web.config文件,还有Web.Debug.config和Web.Release.config,如下图所示:

enter image description here

假设您想要在应用程序的发行版中添加从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 并单击“预览转换”。

enter image description here

您将会看到初始版本和发布版本之间的差异。

enter image description here

参考资料:

免责声明:本指南链接指向我的个人网站。


我没有右键单击并“预览变换”的选项 - 你是如何启用它的? - cvocvo
你使用的 Visual Studio 版本是哪个? - Maytham Fahmi
1
有趣的是我在运行VS 2017时解决了它。我不得不使用<IsTransformFile>True</IsTransformFile>卸载和编辑csproj文件。然后,重新加载csproj文件,预览转换会出现在右键菜单中。更多细节请参见:Preview Transform Menu not showing up for config - cvocvo

10

在创建发布配置时,重写部分对我来说起初表现很奇怪,错误和部分内容根本不显示。以下是我解决问题的方法。

Microsoft (R) Build Engine version 12.0.31101.0

Microsoft .NET Framework, version 4.0.30319.0

编辑 经过一番尝试后,我意识到如果将重写标记放在没有安装重写插件的服务器上,Web服务器会返回错误。我希望在服务器和本地开发机器上有不同的配置,所以解决方法是:

未转换的 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"。

显然,如果你也想在本地机器上使用它,就需要进行更新。


2

可以转换system.webServer的rewrite部分。我最初也遇到了同样的问题,后来发现我不小心将rewrite节点错误地放置在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>

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添加到所有请求中。

-------更新-----

仅仅添加名称到操作中并不能按照预期工作,因此我更新了代码如下:

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接