我正在用新的 MVC 应用程序替换旧的 ASP.NET Webforms 应用程序。但是,我遇到了一个问题,即用户拥有指向特定页面的旧链接,我希望将其自动转换为正确的 MVC 路由。
旧站点:http://mysite.com/ticketsdetail.aspx?id=12345
新站点:http://mysite.com/tickets/details/12345
MVC 路由中是否有一种方法可以捕获旧 URL 并将其转换为新 URL?
编辑:
好的,使用 IIS7 URL 重写执行此操作的 web.config 条目是:
<rewrite>
<rules>
<rule name="Ticket page redirect" stopProcessing="true">
<match url="ticketsdetail.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=(\d*)$" />
</conditions>
<action type="Redirect" url="Calls/Tickets/{C:1}" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
最佳答案
我的建议是不要在代码中执行此操作,除非您绝对需要这样做。 Scott Hanselman 有一篇很好的文章,介绍了如何使用 IIS Url Rewrite 在 web.conf 中执行您需要的操作。 Article Here
这也将是您的 web.config 中的规则:
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^ticket/details/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="ticketdetails.aspx?id={R:1}" />
</rule>
关于asp.net-mvc - 将 ASP.NET Webforms url 转换为 MVC 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3156906/