【问题标题】:With a web.config transform, how do you inject a canonical url rule at the top?使用 web.config 转换,如何在顶部注入规范的 url 规则?
【发布时间】:2013-04-13 03:10:28
【问题描述】:

在 ASP.Net 项目中,对于发布版本的 Web.Config(使用 Web.Release.Config 转换文件),如何在重写部分注入规范的 url 规则?

【问题讨论】:

    标签: asp.net web-config configuration-files web-config-transform


    【解决方案1】:

    下面是一个对我有用的示例,您必须使用 XPath 选择器才能通过 xdt:Transform 属性将您的规则注入到正确的位置。

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="CanonicalHostNameRule1" enabled="true" stopProcessing="true" 
                        xdt:Transform="InsertBefore(/configuration/system.webServer/rewrite/rules/rule[position() = 1])"
                    >
                        <match url="(.*)" />
                        <conditions>
                            <add input="{HTTP_HOST}" negate="true" pattern="^www\.yoursite\.com$" />
                        </conditions>
                        <action type="Redirect" url="http://www.yoursite.com/{R:1}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>

    您也可以使用 xpath 语句进行一些其他有趣的替换。我希望上面的示例对我有所帮助,因为 StackOverflow 通常是我最近寻找这类东西的地方。

    【讨论】:

      猜你喜欢
      • 2015-07-27
      • 2020-10-11
      • 2013-08-27
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 2014-07-03
      • 2012-08-05
      • 1970-01-01
      相关资源
      最近更新 更多