Convert ASP.NET Webforms URL to MVC Route

I am replacing an old ASP.NET web form application with a new MVC application. However, I have a problem with users who have old links to a specific 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 MVC routing to catch the old url and translate it to the new one?

EDIT:

Ok, the web.config entry for this using rewrite of the IIS7 URL:

<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> 
+4
source share
2 answers

Do not do this in code, this is my suggestion if you absolutely do not need to. Scott Hanselman has a good article on how to do what you need in web.conf using IIS Url Rewrite rewrite. Article here

This will be your rule, which will also be available in your 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> 
+2
source

If you are using IIS7, then the URL rewrite tool is pretty smooth; 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 to use?

What does all this information mean if you need to choose the technology to include clean URLs for your application network? In this section, we explain how to make this choice.

If your web application was created using anything other than ASP.NET, use the IIS URL Rewrite. Otherwise, the Rules:

  • 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 the built-in support for clean URLs, including the creation of clean URLs to link to your web pages. Note that ASP.NET routing does not support standard Web Forms applications, although there are plans to support it in the future.
  • If you already have an outdated ASP.NET web application and do not want to change it, use the URL Rewrite module. The Rewrite URL module allows you to translate search engines URLs in the format that the application is currently using. In addition, it allows you to create redirect rules that can be used to redirect search engine crawlers to clean URLs.

However, it’s logical for me to make the URL redirect deal with similar material at the iis level, especially since you will need to use a regular expression to pull the chain to form a new URL. Something a rewriter does surprisingly well.

0
source

Source: https://habr.com/ru/post/1314416/


All Articles