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

LDJ*_*LDJ 47 asp.net iis web-config transform url-rewriting

我有一些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>
Run Code Online (Sandbox Code Playgroud)

我部署到测试时,我的应用程序设置正在转换,但是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)">
Run Code Online (Sandbox Code Playgroud)

但这再次没有区别.

是否有可能替换web.config中的重写规则,如果是这样,我错过了什么?

Stu*_*rtQ 48

因为我的主web.config中没有任何重写规则,所以替换转换不起作用.我成功使用了Insert变换,如下所示:

  <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>
Run Code Online (Sandbox Code Playgroud)


Cry*_*pth 7

在创建发布配置,错误和根本没有显示的部分时,重写部分首先对我很奇怪.这就是我解决它的方式.

Microsoft(R)Build Engine版本12.0.31101.0

Microsoft .NET Framework,版本4.0.30319.0

编辑在弄乱了这个后,我意识到在没有重写插件的服务器上使用重写标记会使web服务器返回错误.我想在服务器和本地开发机器上配置不同,所以修复方法是:

未转换的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>
Run Code Online (Sandbox Code Playgroud)

该操作根本不需要名称,但重写标记需要xdt:Transform ="Insert"

显然,如果您想在本地计算机上使用它,则需要更新.


may*_*ʎɐɯ 6

这里有很多答案,这些例子很好,但我认为缺少一些细节.我在我的网站上写过这个,这里的关键点是添加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>
Run Code Online (Sandbox Code Playgroud)

因此,下次发布项目时,带有重写的标记及其子内容将添加到web.config文件中.

要在发布之前查看,请右键单击Web.Release.config,然后单击"预览变换".

在此输入图像描述

您将看到初始版本和发行版本之间的区别.

在此输入图像描述

参考:

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


J.R*_*ong 3

可以改造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>
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

30505 次

最近记录:

7 年,11 月 前