Understanding ASP. NET 3.5 MVC routing comprehension default route table

Source: Internet
Author: User
Understanding the default route table

When you create a new ASP. net mvc applicationProgramThe application has been configured to use ASP. NET routing. ASP. NET routes are set in two places.

First, enable ASP. net routing in your application web configuration file (Web. config file. In the configuration file, there are four nodes related to routing: sytem. Web. httpmodules, system. Web. httphandlers, system. webserver. modules, and system. webserver. handlers. Be careful not to delete these nodes because they cannot work without routes.

Second, it is more important to create a route table in the global. asax file of the application. The global. asax file is a special file that contains Event Handlers acting on ASP. NET application lifecycle events. The route table is created during the application start event.

The default global. asax file for ASP. net mvc applications is as follows:

 Namespace Mvcapplication { // Note: For instructions on enabling IIS6 or iis7 Classic mode,      // Visit http://go.microsoft.com /? Linkid = 9394801      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); routetable. routes. routeexistingfiles = False ; Routemonitor. routedebugger. rewriteroutesfortesting (routetable. routes );}}}

When an MVC application runs for the first time, the application_start () method is called. This method then calls the registerroutes () method. The registerroutes () method creates a route table.

The default route table contains a route (named default ). The default route maps the first part of the URL to the Controller name, the second part of the URL to the Controller action, and the third part to a parameter named ID.

Suppose you entered the following URL in the address bar of your browser:

/Home/index/3

The default route maps the URL to the following parameters:

Controller = home

Action = index

Id = 3

When you request URL/home/index/3, the followingCode:

Homecontroller. Index (3)

The default route contains the default values of all three parameters. If you do not provide a controller, the default controller parameter is home. If you do not provide an action, the default value of the action parameter is index. Finally, if you do not provide an ID, the ID parameter is a null string by default.

How does the default route map URLs to Controller actions?

Suppose you entered the following URL in the address bar of your browser:

/Home

Because of the default value of the default route parameter, entering this URL will call the index () method of the homecontroller class.

UsingSystem. Web. MVC;NamespaceMvcapplication1.controllers {[handleerror]Public ClassHomecontroller: controller {PublicActionresult index (StringID ){ReturnView ();}}}

The homecontroller class contains a method called index () that accepts a parameter called ID. URL/home will call the index () method and use an empty string as the value of the ID parameter.

The URL/home method also matches the index () method of the homecontroller class in the following way.

UsingSystem. Web. MVC;NamespaceMvcapplication1.controllers {[handleerror]Public ClassHomecontroller: controller {PublicActionresult index (){ReturnView ();}}}

The index () method of the code above does not accept any parameters. URL/home will call this index () method. URL/home/index/3 will also call this method (ID is ignored ).

The URL/home will also match the index () method of the homecontroller class in the following code:

UsingSystem. Web. MVC;NamespaceMvcapplication1.controllers {[handleerror]Public ClassHomecontroller: controller {PublicActionresult index (Int? ID ){ReturnView ();}}}

 

The index () method has an integer parameter. Because this parameter is a void parameter (which can have a null value), you can call index () without causing an error.

Finally, using URL/home to call the index () method in the following code will cause an exception because the ID parameter is not a void parameter. If you try to call the index () method, you will get an error.

  using  system. web. MVC;  namespace  mvcapplication1.controllers {[handleerror]  Public   class  homecontroller: controller { Public  actionresult index ( int  ID) { return  View () ;}}
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: 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.