The problem of adding route instances in the routing table routes collection in asp.net mvc

Source: Internet
Author: User

Yesterday, a classmate asked about adding a custom routing instance to the routes collection, and the problem with her was this:

1. Because you want to add an instance to the routes table when the application is hostile, you need to write the code in the RegisterRoutes method of the global class, and the code is as follows in two ways:

Routes. ADD ("s", New Route ("A/b", New Webformroutehandler ("~/webform1.aspx"));

Routes. ADD ("A", New Route ("c/b", new RouteValueDictionary {{"Controller", "Default"}, {"Home", "Index"}}, New Webformrouteh Andler ("~/webform1.aspx"));

Where A is the name of the route, route is an instance of the route, and a Webformroutehandler custom class is used to process a virtual path and assign it to an actual address, as follows:

public class Webformroutehandler:mvcroutehandler 
        { 
            private string virtualpath; 
            Public Webformroutehandler (string path) 
            { 
                virtualpath = path; 
            } 
     
            protected override IHttpHandler Gethttphandler (RequestContext requestcontext) 
            { 
                var page = Buildmanager.createinstancefromvirtualpath (VirtualPath, typeof (Page)) as IHttpHandler; 
     
                return page; 
            } 
        

Her problem is that, with the first method above, when doing redirecttoaction (), it will jump directly to the WebForm1.aspx page, while using the second way, then normal, only when accessing the c/b, will access the page.

In contrast to the above two pieces of code, in fact, the second way is just one more example of the RouteValueDictionary class, Microsoft's official definition of this example:

Represents a collection of case-insensitive key/value pairs that you can use in different locations in the routing framework (for example, when you define a default value for a route or when you generate a URL that is based on a route).

In fact, the creation of this object, can be a URL that is c/b defined a default access rules, then the URL will certainly follow this rule to access the specified page.

This article is from the "Zhang Jian Technical column" blog, please be sure to keep this source http://jianle.blog.51cto.com/429692/727680

Related Article

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.