ASP. net mvc routing mechanism, asp. netmvc

Source: Internet
Author: User

ASP. net mvc routing mechanism, asp. netmvc
Traditional ASP. NET web form is hypothetical. There is a certain association between the user request URL and the files on the server. Here, the server's job is to follow the user request received, the corresponding file is retrieved to the user. This method is suitable in the web form era, because ASP. NET pages are aspx pages and can independently reply to users' browser requests. However, this method is not suitable in MVC. In MVC, user requests are handled through the methods in the Controller. In MVC, there is no ASP. to solve this problem, we need to learn the MVC routing mechanism. The routing mechanism has two functions: 1. check the received URL request to determine the method of the Request controller. 2. generate external URLs (when a user clicks a link, a request is sent to the URL displayed on the browser through the view)

Now let's get started with MVC routing configuration:

In the MVC framework, there are two ways to create a route:

1. Agreed-based routing configuration

2. Specific route Configuration

You may already be familiar with the agreed-based routing configuration, but the specific routing configuration is newly added in MVC5. We will all learn here

The routing mechanism is unknown. What is the Controller and the Actions method? It just extracts URL fragments, and route requests are processed later, the requested page can be obtained only when the routing configuration is met;

By default, the route matches the URL with the correct segment, for example, {controller}/{action}, and matches the URL with two segments.

The URL mode is conservative and only matches URLs with the same segments. However, the URL mode is inclusive. As long as the URL has a correct segment, the value of the segment will be checked, but no matter what the value is.

 

The route configuration is in the RouteConfig. cs file;

public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }                                           );            routes.MapRoute(                name: "Student",                url: "student/{id}",                defaults: new { controller = "Student", action = "Index"},
                  constraints: new { id = @"\d+" }
); }

 

The static method RegisterRoutes is called in the Global. asax file. Each time the program runs, it will execute the Application_Start () method in the Global File for Route registration;
  protected void Application_Start()        {                        AreaRegistration.RegisterAllAreas();            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);            RouteConfig.RegisterRoutes(RouteTable.Routes);            BundleConfig.RegisterBundles(BundleTable.Bundles);        }

 

 
 

,

 

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.