Study Notes of pro ASP. net mvc 3 framework 21 [best practices of area and URL architecture]

Source: Internet
Author: User
Tags actionlink

Use Areas

The MVC Framework supports organizing a web application in areas (region ).ProgramEach area presents a function segment of the application, such as management, orders, and customer support. This is very useful for a large project, because all controllers, models, and views are placed in a folder (All controllers of the entire project are in a folder) it will be very difficult to manage. In this case, the area is very useful. Each area has its own file structure, such as controller, view, and model, which can be used to maintain relative separation. In this way, every functional block of the program can be more clearly divided. When many developers cooperate in development, conflicts can also be avoided.

An instance is created to describe the role of the Area:
Create a workingwithareas project to use the Internet application template, and then create an area named admin.

After that, we will find that the area is actually a mini version of MVC project, and a class adminarearegistration is generated under the area directory. A very interesting method in this class is registerarea, in this method, a route is registered using the URL mode. Of course, other routes are also defined here. If you want to pass a name to route instead of null, the route name must be globally unique, not only in this area. We do not need to perform other operations to ensure that the registration method is called. The system automatically adds the method to global. asax. the application_start method in CS, for example, arearegistration. registerallareas (); when this is called, the MVC Framework will traverse all the classes of our application, find the classes derived from the arearegistration class, and then call the registerarea method in these classes.

Note: Do not change the sequence of statements related to route in the application_start method. If we call the registerroutes method before calling the arearegistration. registerallareas method, our route should also be defined before area routes. If routes is evaluated or matched in order, this means that the wrong route may be matched when the controller in the area is requested.

The public override void registerarea (arearegistrationcontext context) method in the adminarearegistrationclass has a parameter of the arearegistrationcontext type. This parameter exposes a set of maproute methods for us to use in the area, following our global. asax. the registerroutes method in CS uses the same maproute. This is a bit like we use response in ashx to call it through the httpcontext object. The context object is generally used in this way (personal understanding ). The maproute method in the arearegistrationcontext class automatically limits the namespace registered by route that contains the area controller. This also means that when we create a controller in the area, it must be in the default namespace. Otherwise, the routing system cannot find it.

Then we create a controller-homecontroller in the area, and then create a view. Then we can run our program

The error is reported because MVC does not know which controller to choose (homecontroller has two: one in area and the other out). The solution mentioned in the preceding notes is as follows:

         Public   Static  Void Registerroutes (routecollection routes)
Routes. ignoreroute ( " {Resource}. axd/{* pathinfo} " );

Routes. maproute (
" Default " , // Route name
" {Controller}/{action}/{ID} " , // URL with Parameters
New {Controller = " Home " , Action = " Index " , Id = urlparameter. optional }, // Parameter defaults
New[] {"Workingwithareas. Controllers"}


There are no special steps to create a link in the area as before. The MVC Framework detects requests related to an actual area, and then finds a matching link for the output in the routes defined only for this area.

Example: @ HTML. actionlink ("Click me", "about"), the link displayed on the page is: <a href = "/admin/home/about"> click me </a>
If the link points to an action method in another area or controller outside the area, we must specify the name of the area, as shown below:

@ Html. actionlink ("Click me to go to another area", "Index", new {area = "support "})
Shown as: <AHref="/Support/home"> Click me to go to another area </A>

If we want to create a link, whether the action is in the area but under the root directory can be as follows:

@ Html. actionlink ("Click me to go to another area", "Index", new {Area = ""})

URL architecture best practices

I have talked a lot about the URL structure above. In recent years, the design of the corresponding application URL has been paid more and more attention, and some important designs have emerged. If we follow these strict rules, it will improve application availability, compatibility, and rankings of search engines. These strict rules are as follows:

1. Make our URLs clean and friendly.

Http:// S = books & Ie = utf8 & qid = 1294771153 & sr = 1-13 convert the URL into the following: a lot better.

The following are URL-friendly guidelines:

1) Design a URL to describe its content rather than implementation details. Use "/articles/annualreport" instead of "/website_v2/cachedcontentserver/fromcache/annualreport"

2) The content title is better than the ID. Replace "/articles/annualreport" with "/articles/2392". Of course, you can combine the two statements if necessary, for example,/articles/2392/annualreport, this will input more characters than simply using ID, but this will help the search engine rank.

3) do not use HTML-specific file extensions, such as. aspx or. MVC. However, you can use special file types such as. jpg,. pdf, And. Zip. Although when we set the appropriate mimeys, the browser does not care about the extension name of the file, but the extension name of the file that people refer to depends on.

4) hierarchical. For example,/products/menswear/shirts/red. In this way, the visitor can easily guess the parent-level link.

5) It is case insensitive. This is the default situation for the routing system.

6) Avoid symbols, encoding, and character sequences. If you want to separate a word, you can use "-" For example:/My-great-article. Underlines are unfriendly. URLs encode spaces to become strange characters, such as "/My + great + Article" or annoying characters such as "/My % 20 great % 20article"

7) do not change the URL. Because this is equivalent to losing the business, when we change the URL, we should try to use 301 redirection to support the old URL architecture.

8) maintain consistency. The entire application uses only one URL format.

2. Select get and post correctly.

The general principle is: GET requests should be retrieved for read-only information, and post requests should be any operations that may change the application status.
In the standard commitment terms: Get for secure interaction, post for insecure interaction. These conventions are developed by W3C.
GET requests are addressable-all information is contained in URLs, so this may point bookmarks or links to these addresses.

well, today's note is here. This chapter on URL and routing is now complete.
good night!

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: 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.