Let's talk about routing in ASP. net mvc.

Source: Internet
Author: User

Basically, MVC has been briefly learned. Now let's take a closer look at each module.

Routing in ASP. net mvc is a separate module that plays a major role in the entire ASP. NET architecture and is widely used. MVC is one of them. Our websites and applications are all based on webform. Generally, the URL reflects the structure of the entire website. Then all parameters are passed through querystring or session, because the latter consumes the server memory. If the server is not dispose in time, it will cause a lot of trouble in the future, such as data expiration. The result is that the URL length is getting bigger and bigger, making it difficult for users to know what is being done and what the connection means. Now with this routing, this problem is solved. (In fact, before routing appeared, there were also many solutions to this problem, such as the previous urlrewriting or something. To compare their advantages and disadvantages, I will go into further research in the days to come .)

To construct a good routing, You need to analyze the functions that the system will present to the user. Then, on the basis of summarizing the functions of the application and adding some principles, we can build the application.

Now that the principle of constructing routing is mentioned, I will summarize my ideas:

  1. Keep the URL as short and concise as possible. If the final constructed URL is longer than the webform time, it is better not to do so.
  2. Let the user see the current browsing or operation intention through the URL, that is, let the URL be self-explanatory.
  3. Do not display important IDs in URLs, especially the primary keys of integer variables.
  4. Also, avoid spaces. This is the optimization of the search engine to make your url appear in the search engine better.

After we create an MVC website using the vs template, there will be several lines of code in the globle. asax. CS file:

Public class mvcapplication: system. Web. httpapplication
{
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 = ""} // parameter defaults
);
}

Protected void application_start ()
{
Registerroutes (routetable. routes );
}
}

Briefly. Routetable. routes is a static routecollection object that collects registered route. In the registerroutes method, ignoreroute and maproute are both extension methods. The first is the registration ignore path, followed by the Registration path to be processed.

It should be noted that the order in which the route registration takes effect determines the priority of the route. The higher the priority, the higher the registration priority. Therefore, complicated matching modes should be put at the front, and more general ones should be put at the back.

All in all, routing is the entry to MVC, and mastering its principles will help us expand our programs more easily in the future.

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.