Second urlrouting of the first anniversary of ASP. NET MVC

Source: Internet
Author: User
1. What is urlrouting

You can use urlrouting to configure URL ing so that users can access the website according to your rules.
When using urlrouting, you must specify the URL mode. It includes a location ID that will be returned to you according to this rule when you request a webpage. of course, the created rule is completely defined by you.
The last response shows how to access index. aspx and about. aspx:

HoweverHttp: // localhost/views/home/index. aspx and http: // localhost/views/home/about. aspx 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. Basic urlrouting rule Definition
 Using System; Using System. Collections. Generic; Using System. LINQ; Using System. Web;Using System. Web. MVC; Using System. Web. Routing; Namespace Mvcapplication2 { /// <Summary>      /// It's still the old rule. Check the sequence number.      /// </Summary>      // Note: For instructions on enabling IIS6 or iis7 Classic mode,      // Visit http://go.microsoft.com /? Linkid = 9394801      Public   Class Mvcapplication: system. Web. httpapplication { // 4. Note: change the rule of 1标1 to "{controller}. MVC/{action}/{ID }".         // The IIS6 and iis7 modes are supported.          // Note: General virtual hosts do not support. MVC and. aspx check the file existence.          // The work und is to change. MVC to. ashx or. asbx.          Public   Static   Void Registerroutes (routecollection routes ){ // 5. This path is not controlled by subsequent rules Routes. ignoreroute (" {Resource}. axd/{* pathinfo} "); // 1. You can recall the definition of controller and action.              // The biggest difference between MVC and traditional aspx is that access is              // The accessed controller. Action is a function under a certain type rather than An ASPX file,             // The Controller determines which aspx file to display to the user (that is, the default rule of the same name)              // This is the default urlrouting rule of the file, which is used to access the Controller/Action/ID.              // The mode points to the corresponding controller and action Routes. maproute (" Default ", // Route name " {Controller}/{action}/{ID} ", // URL parameters                  New {Controller =" Home ", Action =" Index ", Id =" "} // 2. The default value of the parameter, that is, access if each part is empty                  // The index action under homecontroller );} Protected   Void Application_start (){ // 3. There is nothing to talk about, that is, the application.ProgramInitialize it at startup Registerroutes (routetable. routes );}}}

Note that in ASP. net mvc, urlrouting only correspondsController/ActionRelated

Iii. Problems solved by urlrouting

Urlrouting appears 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. The {controller}/{action}/{ID} rule defined in global. asax. CS in the example is used as the column.

/Home/about/12 is actually access

Controller = "home" Action = "about", one of its querystring parameters is id = "12"

Iv. urlrouting

System. Web. Routing

System. Web. Routing

System. Web. routing instructions

 

So how can we use ASP. net mvc to compile the desired page?

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.