Convert ASP.NET Webforms url to MVC route
I'm replacing an old ASP.NET webforms app with a new MVC application. However, I have an issue with users having old links to a particular page that I would like to automatically translate to the correct MVC route.
Old site: http://mysite.com/ticketsdetail.aspx?id=12345
New site: http://mysite.com/tickets/details/12345
Is there a way in the MVC routing of catching the old url and translating it to the new one?
EDIT:
Ok, the web.config entry to do this using the IIS7 URL rewriting is:
<rewrite>
<rules>
<rule name="Ticket page redirect" stopProcessing="true">
<match url="ticketsdetail.aspx$" /&g开发者_运维技巧t;
<conditions>
<add input="{QUERY_STRING}" pattern="id=(\d*)$" />
</conditions>
<action type="Redirect" url="Calls/Tickets/{C:1}" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
Don't do it in code is my suggestion, unless you absolutely need to. Scott Hanselman has a good article on how to do what you need in the web.conf using IIS Url Rewrite. Article Here
This would be your rule that would go in your web.config as well:
<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>
If your using IIS7 then the URL rewrite tool is pretty slick; http://learn.iis.net/page.aspx/460/using-url-rewrite-module/
This is a good article; http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
Which Option Should You Use?
What does all this information mean if you need to choose a technology to enable clean URLs for your Web applications? In this section, we explain how to make this choice.
If your Web application is built by using anything except ASP.NET, use the IIS URL Rewrite module. Otherwise, the rules are:
- If you are developing a new ASP.NET Web application that uses either ASP.NET MVC or ASP.NET Dynamic Data technologies, use ASP.NET routing. Your application will benefit from native support for clean URLs, including generation of clean URLs for the links in your Web pages. Note that ASP.NET routing does not support standard Web Forms applications yet, although there are plans to support it in the future.
- If you already have a legacy ASP.NET Web application and do not want to change it, use the URL Rewrite module. The URL Rewrite module lets you translate search engine-friendly URLs into a format that your application currently uses. Also, it lets you create redirect rules that can be used to redirect search engine crawlers to clean URLs.
However, its logical to me to get the url rewriter to sort out this kind of stuff out at the iis level, especially since you'll be needing to use a regex to pull the querystring apart to form the new url.. Something the url rewriter does amazingly well.
精彩评论