ASP. net mvc Framework experience (5): URL Routing)

Source: Internet
Author: User
Overview

In traditional ASP. NET applications, a request is mapped to a. aspx file and corresponds to a specific physical aspx file. ASP. net mvc Framework, after entering a request, it is no longer mapped. the aspx file is mapped to the correct Controller and Action execution request by the path selection engine provided by the MVC Framework. ASP.. net mvc Framework URL path ing rules are defined in Global. in asax, this brings a lot of flexibility in a certain program. If you want to modify the URL application structure, you only need to modify the ing rules and do not need to modify the code in the Controller and View, however, this is not flexible enough. Changing the Global. asax code will still cause application re-compilation. At the end of this article, we will introduce how to use HttpModule to define the ing rules in Web. config.

Understanding Path Selection

First, let's look at the following figure:

 

From this, we can see that after a request occurs, the first step is to select the path, and the ing rule is registered by adding a Route instance to the Routes set, for example:

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

Route has four attributes, 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; }}

Where:

The Url specifies the Url matching rule of the request. It also defines how the URL should be divided into different parameters (tokenized) and the parameters that can be replaced in the URL, it is defined by the syntax of [parameter name.

The ults attribute defines a default value dictionary, which can be used when the incoming URL does not contain a specified parameter value.

The RouteHandler attribute defines the IRouteHandler instance that is used to process requests after the URL is divided into parameters and the appropriate path selection rule is determined.

The Validation attribute allows us to specify a prerequisite for matching a path selection rule. For example, we use a regular expression to filter whether a path selection rule matches a parameter value.

Differences between II6 and II7

When using ASP. net mvc Framework, pay attention to the differences between II6 and II7. If you do not need to use the. mvc extension in II7, the path selection rules can be as follows:

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

In section II6, the Controller name must be followed by the. mvc extension:

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

Verify the path selection rule

In section 2 of this article, we mentioned a Validation attribute of Route, which allows us to specify a prerequisite for matching a path selection rule. As shown in the following code snippet, the verification Id must be an integer and the length must be between 1 and 8:

RouteTable.Routes.Add(            new Route            {                Url = "Blog.mvc/Detail/[id]",                Defaults = new { controller = "Blog", action = "Detail" },                Validation = new { id=@"\d{1,8}" },                RouteHandler = typeof(MvcRouteHandler)            });

Custom RouteHandler

ASP. net mvc Framework provides excellent extension functions. For example, you can customize RouteHandler to configure ControllerFactory and ViewFactory in Web. config. In ASP. net mvc Framework, custom RouteHandler only needs to implement the IRouteHandler interface and implement the GetHttpHandler method. Its definition is as follows:

public interface IRouteHandler{    IHttpHandler GetHttpHandler(RequestContext requestContext);}

Fredrik wrote a complete example on its Blog. If you are interested, refer to it.

Define path selection rules in Web. config

ASP.. net mvc Framework. in the Application_Start method in asax, when the ing rule changes, if the code in Application_Start is modified, the whole application will be re-compiled, we can use HttpModule to place the ing rules in the configuration file. The sample code is as follows:

public class RouteBuilder : IHttpModule{    public void Init(HttpApplication application)    {        RouteConfiguration routeConfig =            (RouteConfiguration)System.Configuration.ConfigurationManager.GetSection("RouteTable");        foreach (RouteElement routeElement in routeConfig.Routes)        {            Route currentRoute = new Route();            currentRoute.Defaults = new DefaultsType(routeElement.Defaults);            currentRoute.Url = routeElement.Url;            currentRoute.RouteHandler = typeof(MvcRouteHandler);            RouteTable.Routes.Add(currentRoute);        }    }    public void Dispose()    {    }}

On CodePlex, you already have an open-source project at http://www.codeplex.com/routebuilder. For more information, see references.

Conclusion

About ASP. the path selection in the net mvc Framework is briefly introduced here. In fact, the next two sections of this article are to recommend some learning materials, and the details about path selection are as follows, I have carefully written ScottGu's article.

Finally, I made another small advertisement: I created a Web Technology Alliance group in the blog community. welcome to join. At the same time, the group will have some activities in the near future to improve the community atmosphere :)

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.