2. ASP. net mvc Routing

Source: Internet
Author: User

When you create a new MVC project and run it directly, you will see the welcome homepage. But when you go to the project, you will find that there is no corresponding page in the project?

That is, MVC Routing is working. Open Global. asax and you can see the corresponding Routing code.

Code Public static void RegisterRoutes (RouteCollection routes)
{
Routes. IgnoreRoute ("{resource}. axd/{* pathInfo }");


Routes. MapRoute (
"Default", // custom name
"{Controller}/{action}/{id}", // Parameter
New {controller = "Home", action = "Index", id = UrlParameter. Optional} // default value of the Parameter
);

}

After the program runs, it will take the default value, go to the address http: // localhost: 1234/Home/Index.

There is an agreement that everyone should follow.

Home indicates that the address is protected against the Controller file HomeController. cs;

Index indicates the Index method in the HomeController. cs file to be executed.

Custom rules:

Routes. MapRoute (
"Shop ",
"Shop/{action}/{year }",
New {controller = "shoping", action = "Index", year = 2000 },
New {year = "\ d {4}"}); // you can use a regular expression to restrict the year.

 

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.