ASP. net mvc-routing of old URLs

Source: Internet
Author: User
ArticleDirectory
    • Final Thoughts

Original article address: ASP. net mvc-Legacy URL Routing
Author: matthaw

URL: ASP. net mvc-routing of old URL
Author: Q. Lee. Lulu

Recently, we have converted most of our ASP. NET web form pages to ASP. net mvc. When this is not a small project, it is necessary to use a new URL structure and support the old URL. This idea is that when you click a page that does not exist, you will be positioned in the appropriate controller and action in MVC.

Process

1. An old form of URL request from your website. For example: http://www.server.com/Users/Login.aspx
2. ASP. NET routing intercepts this request and matches a route in your routecollection
3. Instead of using mvcroutehandler, The legacyroutehandler is called.
4. Use legacyroutehandler to generate an mvc url using the route redirection name you specified, and send an HTTP 301 request directed to http://www.server.com/site/login.

Routing

First, we need to define our old version route class. This is required because we need to expose an additional attribute to allow our routing processor to find the correct MVC route.

  // The route class of the old URL format, exposing a redirectactionname   Public   class  legacyroute: Route { Public  legacyroute ( string  URL,  string  redirectactionname, iroutehandler routehandler):  base  (URL, routehandler) {redirectactionname = redirectactionname;}  Public   string  redirectactionname { Get ;  set ;}

Then, we need to define the route handler and the associated HTTP handler. Route handler inherits from iroutehandler, which is used when you create your old URL routing. HTTP handler inherits from mvchandler because it provides some key information, such as requestcontext. You may notice that you need to copy all the querystring parameters from the request (Code). This is a required step, because the getvirtualpath method requires all route data (from routedata. values) and tries to use it to create a URL.

 // The legacy route handler, used for getting the httphandler for the request  Public   Class Legacyroutehandler: iroutehandler { Public Ihttphandler gethttphandler (requestcontext ){ Return   New Legacyhandler (requestcontext )}}
// The legacy httphandler that handles the request Public Class Legacyhandler: mvchandler { Public Legacyhandler (requestcontext ): Base (Requestcontext ){} Protected Override Void Processrequest (httpcontextbase httpcontext ){ String Redirectactionname = (legacyroute) requestcontext. routedata. Route). redirectactionname; //... Copy all of the querystring parameters and put them within routecontext. routedata. Values Virtualpathdata DATA = routetable. routes. getvirtualpath (routecontext, redirectactionname, routecontext. routedata. values); httpcontext. Status =" 301 moved permanently "; Httpcontext. appendheader (" Location ", Data. virtualpath );}}

Finally, you need to create your routes in the global. asax file. Remember, this is a required step When configuring routing.

Public VoidRegisterroutes (routecollection routes) {routes. maproute ("Login","Site/login",New{Controller ="Users", Action ="Displaylogin"}); Routes. Add ("",NewLegacyroute ("Users/login. aspx","Login",NewLegacyroutehandler ()));}

 

That's it. When a request comes, you will see the following information in Fiddler:

    1. A request on "users/login. aspx"
    2. A HTTP 301, with a header "location" and Value of "SITE/login"
    3. A request on "SITE/login"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.