Document directory
- 1. What is URL routing?
- Ii. Definition of basic URL routing rules
- Iii. Problems solved by URL Routing
- Iv. url Routing
[ASP. NET topics] (1) -- A Preliminary Study of ASP. net mvc: http://blog.csdn.net/rocket5725/archive/2010/01/11/5177320.aspx
There is a question: how can we see the index. aspx and about. aspx pages?
The answer is that we can use URL routing to configure URL ing so that users can access the website according to your rules.
1. What is URL routing?
To use URL routing, you must specify the URL mode. It includes a location ID that will be returned to you when you request a webpage according to this rule. of course, the created rule is completely defined by you.
In the previous example, how to access index. aspx and about. aspx?
However, http: // localhost/views/home/index. aspx and http: // localhost/views/home/about. aspx addresses cannot directly access the following two files: views/home/index. aspx and views/home/about. aspx
What is the problem? How can we access it?
The answer is: http: // localhost/home and http: // localhost/home/about.
Why?
This is the urlrouting function, and the configuration of this function is defined by the URL request rule, which is defined in global. asax. CS.
Ii. Definition of basic URL routing rules
We can see the code of Global. asax. CS, as follows:
Using system; <br/> using system. collections. generic; <br/> using system. LINQ; <br/> using system. web; <br/> using system. web. MVC; <br/> using system. web. routing; </P> <p> namespace mvcapplication2 <br/> {<br/> /// <summary> <br/> // still old rule, view by sequence number <br/> /// </Summary> <br/> // Note: For instructions on enabling IIS6 or iis7 Classic mode, <br/> // visit http://go.microsoft.com /? Linkid = 9394801 <br/> public class mvcapplication: system. web. httpapplication <br/>{< br/> // 4. note: Change the 1标 rule to "{controller }. MVC/{action}/{ID} "<br/> // You can manually support the IIS6 and iis7 modes <br/> // note: generally, virtual hosts do not support this function. MVC ,. aspx also needs to check whether the file exists <br/> // The work und is. change MVC. ashx or. asbx </P> <p> Public static void registerroutes (routecollection routes) <br/>{< br/> // 5. This path is not controlled by subsequent rules <br/> routes. ignoreroute ("{resource }. axd/{* pathinfo }"); <br/> // 1. You can recall the definition of controller and action. <br/> // The biggest difference between MVC and traditional aspx is that access is <br/> // the access controller. action is a function under a certain type rather than An ASPX file. <br/> // The Controller determines which aspx file is to be presented to the user (that is, the default rule of the same name) <br/> // This is the default urlrouting rule of the file, is to direct the access to the Controller/Action/ID <br/> // mode to the corresponding controller and action <br/> routes. maproute (<br/> "default", // route name <br/> "{controller}/{action}/{ID }", // URL parameter <br/> New {controller = "home", Action = "Index ", id = ""} <br/> // 2. The default value of the parameter is to access the action of index under homecontroller if each department is empty. <br/> // the action of index under homecontroller <br/> ); <br/>}< br/> protected void application_start () <br/> {// 3. this is nothing to mention, that is, initializing it when the application starts <br/> registerroutes (routetable. routes); <br/>}< br/>}
Note that urlrouting in ASP. net mvc is only related to Controller/action.
Iii. Problems solved by URL Routing
Urlrouting is used to make the URL shorter and more meaningful. For example: Post. aspx? The parameter URL of year = 1999 & month = 3 & day = 8 is changed
/Post/1999/3/8/such a brief, beautiful, and meaningful URL
Similar to home/about. aspx? Id = 12, in the example of global. asax. the {controller}/{action}/{ID} rule defined in CS is column/home/about/12, which is actually one of the querystring parameter is id = "12"
Iv. url Routing
To respect originality, the following address is provided:
System. Web. Routing
System. Web. Routing
System. Web. routing instructions