ASP. NET has no magic-ASP. net mvc routing, asp. netmvc

Source: Internet
Author: User

ASP. NET has no magic-ASP. net mvc routing, asp. netmvc

The previous article introduced the development of the My Blog article maintenance function. during the development process, the Area method was used to establish the Controller, View, and Model used to maintain the article. However, no matter how the code changes, it is accessed by a url in the browser. Currently, My Blog has the following available urls:
Http: // localhost: 52356/-- Home Page
Http: // localhost: 52356/Home -- Home Page
Http: // localhost: 52356/Post -- Blog list
Http: // localhost: 52356/Post/Get/1 -- Blog content with ID 1
Http: // localhost: 52356/Home/About -- website information

Http: // localhost: 52356/Admin/Home -- manage the Home Page
Http: // localhost: 52356/Admin/PostManagement -- manage the blog list
Http: // localhost: 52356/Admin/PostManagement/Update/1 -- Update the Blog content with ID 1.
Http: // localhost: 52356/Admin/PostManagement/Insert -- add an article

You can see from the list above that you can access the Home page through two addresses, but the management Home page only has one address. Can you omit Home like the Home page? Error? Why? In addition, why do I need the Action name to add or modify an article, but the Index on the list page does not?

  

Depending on the URL, the routing mechanism of ASP. net mvc determines which controller is executed. This article will introduce the Routing Mechanism of ASP. NET from the following points:

● Web Server static resource access
● Routing Mechanism in ASP. NET
● Route table
● Route Registration
● Route registration parameters
● Area Routing

Web Server static resource access

When accessing a static file on an HTTP server, it is generally accessed through the domain name + file relative path, such as the first article list page written in html:

  

Routing Mechanism in ASP. NET

In ASP. NET provides a Routing mechanism, which analyzes the url to determine the request to the appropriate location, no longer need to specify the physical location, but ASP. among the three Web frameworks of NET, the routing mechanism is also mainly used in the MVC Framework. Other Web forms and Web pages are still commonly accessed through physical addresses (Note: ASP. NET ).

Route table

ASP. the reason why routing can be used in. NET is an ASP. NET application has a route table (RouteTable) in the RouteConfig under the App_Start directory of My Blog project. the parameter of the Registry routing (RegisterRoutes) method in the cs file is the route table, which is a System. web. routing. routeCollection type, which is defined as follows:

  

  

Note: There is a MapPageRoute method in the definition of the route table. This method is used to register a route for the Web Forms application. The parameter shows that a physical file path is matched by specifying a routeUrl.

Where is the MVC route registration method? MVC routing is only an extension of the route table. It is located in the namespace of System. Web. Mvc:

  

 

Route Registration

Return to the method used to register a route in the RouteConfig class. This method has the following functions:

   

1. Add a route entry named Default.
2. routes are used to match URLs in the {controller}/{action}/{id} mode.
3. the default values of controller and action are Home and Index, and id is optional (this explains why http: // localhost: 52356/can access the Index Action of HomeController ), you do not need to provide the Index Action when accessing the list page, because it is already set to Index by default.
4. namespaces specifies that only the namespace matches the Controller of the namespace under My_Blog.Controllers (it solves the problem of duplicate names of the Home Controller ).

Route registration parameters

1. name:

The unique identifier of the route information. When two routes with the same name are registered, an exception is thrown.
2. url:

It is a url template. Its format is the same as that of a url. It is separated by a slash (/) into multiple paragraphs. Each paragraph can be composed of text and variable placeholders, variable placeholders use braces {} to expand the variable name. The text cannot contain special characters. Multiple variables can be defined in a paragraph, but they must be separated by text. For example,/{controller}-{action}/is incorrect. In mvc, the required variables include {controller} and {action}. What about {id? Every time you create a routing registration template for the default MVC project, is it necessary to have the id variable?
Create an experiment and delete the {action} variable in the url template:

  

After running the program, the program can run, but the following two problems occur:
1). the link action parameter created using Html. ActionLink is invalid. Only one blank connection is generated:

  

  

2). All connections except index action cannot be opened:
Http: // localhost: 52356/Home/About is recognized as http: // localhost: 52356/Home/, and the default index is added to jump to the Home page.
The default parameters of Post are not set and cannot be found directly:

  

3) when the default action parameter is deleted, an exception is thrown when the application is started:

  

  

Note: deleting {id} or changing its name will not be wrong, but will only affect the parameter binding of action.

3. defaults:

The default value of the route. Note that it is not only the default value of the variable of the routing template, but also the routing template is included in the route value set. This means that after the action variable of the url target is deleted in the above experiment, its default value is still valid and will be detailed later.

4. namespaces:
A namespace array indicates that this route matches only the controllers in these namespaces. Avoid multiple modules with the same name as the Controller. Therefore, if an application has multiple modules, the best way is to add the namespace of the Controller of the module to the routes of each module to avoid conflicts.

5. constraints:
The MapRoute method also has a constraints parameter, which is a set of regular expressions used to verify whether the parameters in the url meet the expression requirements.

About Area Routing

Area is used by MVC to separate functions. After an Area is added to an MVC application through VS, A {Area name} AreaRegistration is automatically added. cs file. This file contains the route registration for this Area. Its url template is hard-coded according to the name of the area:

  

This route matches only the url starting with Admin.
If there is a possibility of duplicate names, it is best to add the namespace of the Controller for this Area.

How to Use routing in ASP. NET MVC

The preceding describes how to register a route and its parameters. According to the sample code, you only need to provide the default values of url templates and variables for registering a route in MVC, if the application may have a Controller with the same name, you only need to add the namespace. Here is a brief summary:
● The {controller} and {action} variables must be set in the url template of ASP. net mvc routing.
● You can set the default value for the route value (including template variables) through the defaults parameter in the route.
● If there is a naming conflict, you can solve it through the namespaces array.
● If area is used for separation, pay attention to the automatically generated url template. If there may be naming conflicts, you can use the namespaces attribute.
● The route table is ordered. After a route is matched, subsequent routes will not be matched. Therefore, pay attention to the order to avoid being routed to the wrong processor (note: about the processor ).

Summary

This chapter briefly introduces ASP. NET routing mechanism, and the methods and parameters used to register routing in the MVC program, and summarizes some routing usage methods. In the subsequent articles, we will further introduce the routing working mechanism.

Refer:

Https://msdn.microsoft.com/en-us/library/cc668201.aspx#setting_default_values_for_url_parameters

Connection: http://www.cnblogs.com/selimsong/p/7660176.html

ASP. NET has no magic-directory

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.