ASP. net mvc routing Overview (C #)

Source: Internet
Author: User

In this tutorial, we will introduce you to the important features of every ASP. net mvc program, calledASP. NET routing.The ASP. net routing module maps incoming browser requests to a specific MVC controller actions. At the end of the tutorial, you will understand how the standard route table maps requests to the Controller action.

Use the default route table

Create an ASP. net mvc program. It has been configured to use ASP. NET routing. ASP. NET routing is created in two places.

First, ASP. NET routing is enabled in the Web configuration file (Web. config file) of the program. The configuration file contains four nodes that correspond to the route: system. Web. httpmodules, system. Web. httphandlers, system. webserver. modules, and system. webserver. handlers. Be careful not to delete these nodes because they can no longer work without routing.

The second and most important one is that the route table is created in the program's global. asax file. The global. asax file is a special file, which includes the processing program of ASP. NET program lifecycle events. The route table is created in the Application Start event.

Code 1 contains the default global. asax file of the ASP. net mvc program.

Code 1-Global. asax. CS

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.Routing;namespace MvcApplication1{    // 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);        }    }}

When the MVC program is started for the first time, the application_start () method is called. Instead, this method calls the registerroutes () method. Create a route table using the registerroutes () method.

The default route table contains a simple route named default ). The default route maps the first segment of the URL to the Controller name, the second segment to the Controller action, and the third segment toID.

Imagine entering the following URL in your browser's address bar:

/Home/index/3

The default route maps the URL to the following parameters:

    Controller = home

    Action = index

    Id = 3

When you request this URL/home/index/3, the following code will be executed:

Homecontroller. Index (3)

The default value is included in all three parameters. If you do not provide a controller, the default controller parameter isHome.If you do not provide action, the default value of action isIndex.Finally, if you do not provide an ID, the ID parameter is a null string by default.

Let's take a look at some examples of how default routes map URLs to Controller actions. Imagine entering the following URL in your browser's address bar:

/Home

Because of the default value of the default route, this URL will call the index () of the homecontroller class, as shown in Code 2.

Code 2-homecontroller. CS

using System.Web.Mvc;namespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index(string id)        {            return View();        }    }}

In code 2, The homecontroller class contains a method named index (), which accepts a simple parameter named ID. URL/home makes the index () method called when the value of the ID parameter is a null string.

Because the MVC Framework calls the Controller actions method, URL/home also matches the index () method of homecontroller class in code 3.

Code 3-homecontroller. CS (index action without parameter)

using System.Web.Mvc;namespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index()        {            return View();        }    }}

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

URL/home also matches the index () method of the homecontroller class in code 4.

Code 4-homecontroller. CS (index action with a null type parameter)

using System.Web.Mvc;namespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index(int? id)        {            return View();        }    }}

 

In code 4, the index () method has an integer parameter. The index () method can be called without generating any errors because this parameter is a void parameter (the value can be null.

Finally, using URL/home to call the index () method in code 5 causes an exception because of the ParameterNoAn empty parameter. If you try to call the index () method, the error shown in Figure 1 is returned.

Code 5-homecontroller. CS (index action with ID parameter)

using System.Web.Mvc;namespace MvcApplication1.Controllers{    [HandleError]    public class HomeController : Controller    {        public ActionResult Index(int id)        {            return View();        }    }}

Figure 01: Call the Controller action with parameters (click to view the full size)

On the other hand, URL/home/index/3 runs well in the index controller in code 5. The request/home/index/3 makes the index () method call the ID parameter with the value of 3.

Summary

This tutorial introduces ASP. NET routing. We checked the default route table obtained by ASP. net mvc program. You learned how to map URLs to control actions by default routing.

Address: http://www.asp.net/learn/mvc/tutorial-05-cs.aspx

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.