iis7 url rewrite for webforms aspx to mvc page

不打扰是莪最后的温柔 提交于 2019-12-11 19:27:15

问题


I have had a website with /contact.aspx which gets a succesful redirect to /contact But I want to redirect my old /content.aspx?contentid=123 to /about/123 Here is my redirect XML part in my web.config:

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

Visiting mydomain.com/content.aspx?contentid=123 gives me a 404. also tried without appendQueryString="false" Seems easy to fix to me, but I am missing something...

using a regex instead of wildcard also gives a 404:

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

回答1:


From IIS rewrite module configuration documentation.

The URL string that is evaluated against the pattern does not include the query string. To include the query string in the rule evaluation you can use the QUERY_STRING server variable in the rule’s condition.

So after a big of test jiggery-pokery, I think this should work:

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


来源:https://stackoverflow.com/questions/31589259/iis7-url-rewrite-for-webforms-aspx-to-mvc-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!