asp.net MVC Framework Experience (5): Path selection (URL Routing)

Source: Internet
Author: User

Overview

In a traditional ASP.net application, a request is mapped to an. aspx file and corresponds to a specific physical ASPX file. In the ASP.net MVC framework, when a request is entered, it is no longer mapped to an. aspx file, but instead is mapped to the correct controller and action execution requests by the path selection engine provided in the MVC framework. The URL path mapping rule in the ASP.net MVC framework is defined in Global.asax, which brings a lot of flexibility to the program, and if you want to modify the URL application structure, you just need to modify the mapping rules. There is no need to modify the code in controller and view, but this is still not flexible enough. Changing the Global.asax code will still cause the application to recompile, and the end of this article will explain how to define the mapping rules by HttpModule in web.config.

Understanding Path Selection

Let's take a look at the following picture:

As you can see from the previous illustration, the first step after a request is a path selection, and the mapping rule is registered by adding a route instance to the routes collection, such as:

RouteTable.Routes.Add (
      new Route
      {
        Url = "[controller]/[action]",
        Defaults = new { action = "Index"},
        RouteHandler = typeof (MvcRouteHandler)
      });

There are four attributes in the route, which are defined as follows:

public class Route
{
  public Route();
   public Route(string url, Type routeHandler);
  public Route(string url, object defaults, Type routeHandler);
  public object Defaults { get; set; }
  public Type RouteHandler { get; set; }
  public string Url { get; set; }
  public object Validation { get; set; }
}

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.