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

Asp.net 在web.config转换中替换IIS重写规则,asp.net,iis,url-rewriting,web-config,transform,Asp.net,Iis,Url Rewriting,Web Config,Transform,我有一些IIS重写规则,我想根据环境的不同而有所不同。开发重写规则在web.config文件中,然后在web.test.config文件的末尾,我有: <appSettings> ...Some app settings tranforms here </appSettings> <system.webserver> <rewrite xdt:Transform="Replace">

我有一些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>
但这也没什么区别


甚至可以替换web.config中的重写规则吗?如果可以,我缺少什么?

可以转换system.webServer的重写部分。我最初也遇到了同样的问题,并意识到我不小心将“重写”节点错误地放在了system.web下。虽然根据您提供的有限代码段,这看起来不像是您的问题,但我仍然怀疑您的问题与转换文件中的节点放置有关

下面是my Web.Debug.config的外观(此版本正在调试版本上编写正确的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=“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>

起初,在创建发行版配置时,重写部分工作得很奇怪,错误和部分根本不显示。我就是这样解决的

Microsoft(R)生成引擎版本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>

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


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

这里有很多答案和示例,这是一件好事,但我认为缺少一些细节。我在我的中已经写过这方面的内容,这里的关键点是在要为相应环境添加的根标记层次结构中添加
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并单击预览转换

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

参考:

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


您能告诉我如何在Parameters.xml文件中执行此操作吗?@batu程序员我不熟悉Parameters.xml文件。我建议你提出一个新问题,详细说明你想要实现的目标。我没有右键单击和“预览转换”的选项——你是如何实现的?你有哪个版本的visual studio?有趣的是,我解决了;跑步VS 2017。我不得不用
True
卸载并编辑csproj。然后我重新加载了csproj,右键单击上下文菜单中出现了预览转换。详情如下:
<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>
<?xml version="1.0"?>

.....
  <system.webServer>
    <rewrite xdt:Transform="Insert">
      <rules>
        ......
      </rules>
    </rewrite>
  </system.webServer>
</configuration>