【问题标题】:iis7 url rewrite for webforms aspx to mvc page将 webforms aspx 的 iis7 url 重写为 mvc 页面
【发布时间】:2015-10-13 20:53:49
【问题描述】:

我有一个带有/contact.aspx 的网站,它成功重定向到/contact 但我想将我的旧/content.aspx?contentid=123 重定向到/about/123 这是我的 web.config 中的重定向 XML 部分:

<rewrite>
    <rules>
        <rule name="contact" patternSyntax="Wildcard" stopProcessing="true">
            <match url="contact.aspx" />
            <action type="Redirect" url="Contact" redirectType="Found" />
        </rule>
        <rule name="Content" patternSyntax="Wildcard" stopProcessing="true">
            <match url="content.aspx?contentID=*" />
            <action type="Redirect" url="About/{R:1}" appendQueryString="false" redirectType="Found" />
        </rule>
    </rules>
</rewrite>

访问 mydomain.com/content.aspx?contentid=123 给我一个 404。 也试过没有appendQueryString="false" 对我来说似乎很容易解决,但我错过了一些东西......

使用正则表达式而不是通配符也会给出 404:

<rule name="Content">
    <match url="content.aspx?contentID=([0-9]+)" />
    <action type="Redirect" url="About/{R:1}" redirectType="Found" />
</rule>

【问题讨论】:

    标签: asp.net-mvc url-rewriting iis-7 url-rewrite-module


    【解决方案1】:

    来自IIS rewrite module configuration documentation.

    根据模式评估的 URL 字符串不包括查询字符串。要在规则评估中包含查询字符串,您可以在规则的条件中使用 QUERY_STRING 服务器变量。

    所以经过大量的测试后,我认为这应该可行:

    <rule name="Content" patternSyntax="ECMAScript" stopProcessing="true">
        <match url="content.aspx" />
        <conditions>
            <add input="{QUERY_STRING}" pattern="contentID=([^&amp;]+)" />
        </conditions>
        <action type="Redirect" url="About/{C:1}" redirectType="Found" appendQueryString="False" />
    </rule>
    

    【讨论】:

    • 似乎几乎正确。我得到:Error?aspxerrorpath=/About/192 当 About/192 确实有效时。
    • 嗯...我想不出为什么,但肯定有一些不同。听起来您打开了自定义错误,您可以将其关闭以查看实际的错误页面吗?
    猜你喜欢
    • 2011-02-16
    • 2011-02-07
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 2011-04-06
    • 2014-07-08
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多