asp.net MVC routing rules and brief description __.net

Source: Internet
Author: User
All of the following custom routing rules must not be effective until the default routing rule

Direct method overload + anonymous Object

The most basic routing rule, and also the default routing rule
Routes. Maproute (
Name: "Default",
URL: ' {controller}/{action}/{id} ',
defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}
);


Constructs a route and then adds
Route Myroute = new Route ("{controller}/{action}", New Mvcroutehandler ());
Routes. ADD ("MyRoute0", Myroute);


To add a route to a namespace

In the action, a parameter "catchall" is defined to receive all the parameter values in the URL except the ID

Catchall This will enable the transfer of multiple parameters

Routing across namespaces
Routes. Maproute (
"Addcontollerroute",
"Home/{action}/{id}/{*catchall}",
New {controller = "Home", action = "Index", id = urlparameter.optional},
New[] {"Urlsandroutes.additionalcontrollers"}
);
Routes. Maproute (
"MyRoute1",
' {Controller}/{action}/{id}/{*catchall} ',
New {controller = "Home", action = "Index", id = urlparameter.optional},
New[] {"Urlsandroutes.controllers"}
);

After adding {*catchall} to this configuration after {ID}, you need to increase the string catchall this parameter after the action's parameter ID to get all the parameters except the ID, as follows:

Public ActionResult abctest (string id,string catchall)
{
string temp = ID;
string tt = Catchall;

return Content (ID);
}


Variable-length routing + regular expression matching routing

//Regular expression, in routing configuration, we have made such a rule that only matches the controller name with H
             routes. Maproute (
               ) MyRoute2 ",
               " { Controller}/{action}/{id}/{*catchall} ",
                 New {controller = "Home", action = "Index", id = urlparameter.optional},
                 New {controller = "^h.*", action = "^index$| ^about$ "},
                new[] { "Urlsandroutes.controllers"}
           );

           //Specify Request methods
             routes. Maproute ("MyRoute3", "{Controller}/{action}/{id}/{*catchall}",
                 New {controller = "Home", action = "Index", id = urlparameter.optional} ,
                New {controller = "^h.*", action = "index| About ", HttpMethod = new Httpmethodconstraint (" Get ")},
                 new[] {"Urlsandroutes.controllers"}
            );

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.