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:
- 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.
- Let the user see the current browsing or operation intention through the URL, that is, let the URL be self-explanatory.
- Do not display important IDs in URLs, especially the primary keys of integer variables.
- 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.