Routing chapter of ASP. NET MVC Learning

Source: Internet
Author: User

Path comparison in 1.URL path and routing configuration

2. From the above figure, we can draw the following set of routing data

3. Two of Righteousness

If we also create a new home controller in models, then you will notice the error after re-flushing. And this is because there is no way to determine exactly which controller to call the reason for the request, of course, you think as long as we do not create the same name of the controller, so you can only control your project does not appear, but you do not have control of your loaded library will not appear, but the ASP. MVC has provided a solution to us, as corrected below

RouteConfig.cs:

public class Routeconfig    {public        static void RegisterRoutes (RouteCollection routes)        {            routes. MapRoute (                name: "Default",                URL: "{controller}/{action}/{id}",                defaults:new {controller = "Home", action = "Index", id = urlparameter.optional},                namespaces:new[] {"Mvcstudy.controllers"}            );        }    }

We can see that the namespaces parameter, by passing the name of the namespace into it, can be a question of excluding ambiguity.

4. Constrained routing

Above we have a {ID} to capture the parameters, but you also found that it can catch any string and so on, but we sometimes need to limit it, such as let it only enter numbers, then we can use the regular expression to constrain it.

public class Routeconfig    {public        static void RegisterRoutes (RouteCollection routes)        {            routes. MapRoute (                name: "Default",                URL: "{controller}/{action}/{id}",                defaults:new {controller = "Home", action = "Index", id = urlparameter.optional},                constraints:new{id = @ "^\d*$"},                namespaces:new[] {" Mvcstudy.controllers "}            );        }    }

 

Routing chapter of ASP. NET MVC Learning

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.