URL path selection rules of ASP. net mvc Framework

Source: Internet
Author: User

In the first part of this series, we created an e-commerce website with three types of URLs:

We create a ProductsController class like the following to process these URLs:

After adding the above class to our application, the asp.net mvc Framework will automatically direct the incoming URL to the appropriate action Method on our controller to process the request.

In today's post, we will discuss in depth how this URL ing HAPPENS, and more advanced path selection routing scenarios that we can use in the asp.net mvc framework. I will also demonstrate how you can easily unit test URL path selection scenarios.

What does the URL path selection system of asp.net mvc Framework do?

The asp.net mvc framework includes a flexible URL path selection system that allows you to define URL ing rules in applications. The path selection system has two main objectives:

Map the incoming URLs to the application and direct them to the application. In this way, the correct Controller and Action method are executed to process these requests.

The build can be used to call back the output URLs of Controllers/Actions to the client, such as form submission, <a href = ""> links, and AJAX calls)

The ability to use URL ing rules to simultaneously process incoming and output URL scenarios adds a lot of flexibility to the application code. This means that if we want to change the URL structure of an application in the future, for example, rename/Products to/Catalog), we can modify a set of ing rules at the application level, you do not need to change any code in the Controller or view template.

Default asp.net mvc URL path selection rules

By default, when you use Visual Studio to create a new project using the asp.net mvc Web Application Template, it adds an asp.net Application class to the project. This is implemented in the Global. asax background code:

The asp.net Application class allows developers to handle Application startup/abort and global error handling logic.

The default asp.net mvc Project template automatically adds an Application_Start Method to the class and registers two URL path selection rules:

The first path selection rule above indicates that, by default, the asp.net mvc framework decides which Controller class is used to generate instances, which Action method is called, and which parameters need to be passed in ), you should map the URL to the controller in the format of "[controller]/[action]/[id.

The default path selection rule is why the URL/Products/Detail/3 requests in our e-commerce browsing routine in the first part automatically call the Detail method of our ProductsController class, the reason why 3 is passed as the id parameter value:

The second path selection rule above is used for the root URL "Default. aspx "is used for special case processing. when processing the request of the root URL of an application, this URL is sometimes transmitted by the server instead ). This rule ensures the root "/Default. aspx "or"/"requests are automatically generated by Visual Studio when we use the asp.net mvc Web Application Project template to generate a new Application) the Index () action Method in.

Understanding Route instances

The path selection rule is registered by adding a Route instance to the Routes set of System. Web. mvc. RouteTable.

The Route class defines many attributes that you can use to configure ing rules. You can set these attributes by assigning values to the "traditional". net 2.0 attributes:

Alternatively, you can use the new object initializer features in the C # and VB compilers of VS 2008 to set attributes more concisely:

The Url attribute of the Route class defines whether a path selection rule applies to the Url matching rules of specific incoming requests. It also defines how the URL should be split into tokenized) different parameters. Parameters that can be replaced in a URL are defined by the [parameter name] syntax. As mentioned in post-literary theory, we are not limited to a set of fixed "well-known" parameter names. You can use any number of parameters in the URL. For example, I can use a "/Blogs/[Username]/Archive/[Year]/[Month]/[Day]/[Title]" URL rule to post a blog separate URLs, the mvc Framework automatically analyzes the parameters UserName, Year, Month, Day, and Title, and passes them into the action Method of my controller.

The ults attribute in the Route class defines a default value dictionary, which can be used when the incoming URL does not contain a specified parameter value. For example, in the preceding URL ing example, we define two default URL parameter values: "[action]" and "[id]". This means that if the application receives the/Products/URL, by default, the system uses "Index" as the action name of ProductsController by default. Similarly, if/Products/List/is specified, the null String is used as the value of the "ID" parameter.

The RouteHandler attribute of the Route class defines the IRouteHandler instance used to process requests after the URL is divided into parameters and the appropriate path selection rule is determined. In the above example, we show that we want to use the System. Web. mvc. mvcRounteHandler class to process the configured URL. The reason for this additional step is that we want to ensure that the URL path selection system can be used for both mvc and non-mvc requests. With this IRouteHandler interface, we can also use it cleanly for non-mvc requests, such as standard WebForms And Astoria REST support ).

The Route class also has a Validation attribute, which will be discussed later in this article. This attribute allows us to specify a prerequisite for matching the path selection rule. For example, we can specify a path selection rule that should only apply to a specific HTTP verb that allows us to easily map REST commands), or we can use regular expressions for parameter values, to filter whether a path selection rule matches.

Note: In the first public preview version of The asp.net mvc Framework, the Route class cannot be extended. It is only a data class.) In the next preview version, we are studying how to make it extensible, allowing developers to add path classes for specific scenarios, such as a RestRoute subclass, to add new semantics and functions cleanly.

Path rule Evaluation

When an incoming URL is received by the asp.net mvc Web application, the asp.net mvc framework evaluates the path selection rules in the RouteTable. Routes set to determine the appropriate Controller to process the request.

The asp.net mvc framework evaluates the Controller to be used based on the order in which the RouteTable rules are registered. Check the incoming URL for each Route rule to see if it matches. If a Route rule matches, then the rule and associated RouteHandler) all subsequent rules used to process incoming requests are omitted ). This means that you generally need to organize your path selection rules in the order of "the most special to the least special most specific to least specific, from special to general.

  1. Asp.net Session model Overview
  2. Introduction to the lifecycle of the asp.net page: stages, events, and others
  3. Introduction to asp.net mvc Lifecycle
  4. Several methods for processing asp.net Session invalidation
  5. Asp.net: all instances share a static variable.

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.