[ASP.NET MVC2 系列] ASP.NET MVC Routing概述 (C#)

 [ASP.NET MVC2 系列] 
     [ASP.NET MVC2 系列] ASP.Net MVC教程之《在15分钟内用ASP.Net MVC创建一个电影数据库应用程序》 
     [ASP.NET MVC2 系列] ASP.Net MVC教程之《ASP.NET MVC 概述》
     [ASP.NET MVC2 系列] 理解MVC应用程序的执行过程
     [ASP.NET MVC2 系列] ASP.NET MVC Routing概述 
     [ASP.NET MVC2 系列] ASP.NET MVC 之如何创建自定义路由约束 
     [ASP.NET MVC2 系列] Action要求与View本质
     [ASP.NET MVC2 系列] Action Filters以及自定义OutputCache ActionFilterAttribute事件发生次序

  

                                            ASP.NET MVC Routing概述
 
       ASP.NET Routing模块的责任是将传入的浏览器请求映射为特有的MVC controller actions。
 
 使用默认的Route Table
       当你创建一个新的ASP.NET MVC应用程序,这个应用程序已经被配置用来使用ASP.NET Routing。 ASP.NET Routing 在2个地方设置。第一个,ASP.NET Routing 在你的应用程序中的Web配置文件(Web.config文件)是有效的。在配置文件中有4个与routing相关的代码片段:system.web.httpModules代码段,system.web.httpHandlers 代码段,system.webserver.modules代码段以及 system.webserver.handlers代码段。千万注意不要删除这些代码段,如果没有这些代码段,routing将不再运行。第二个,更重要的,route  table在应用程序的Global.asax文件中创建。这个Global.asax文件是一个特殊的文件,它包含ASP.NET 应用程序生命周期events的event handlers。这个route  table在应用程序的起始event中创将。

       在Listing 1中包含ASP.NET MVC应用程序的默认Global.asax文件.

Listing 1 - Global.asax.cs

 1 public class MvcApplication : System.Web.HttpApplication
 2     {
 3         public static void RegisterRoutes(RouteCollection routes)
 4         {
 5             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 6             routes.MapRoute(
 7                 "Default"// 路由名称
 8                 "{controller}/{action}/{id}"// 带有参数的 URL
 9                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
10             );
11 
12         }
13 
14         protected void Application_Start()
15         {
16             AreaRegistration.RegisterAllAreas();
17 
18             RegisterRoutes(RouteTable.Routes);
19         }
20     }

 

  

       当一个MVC应用程序第一个启动,Application_Start() 方法被调用,这个方法反过来调用RegisterRoutes() 方法。

       这个默认的route  table包含一个单一的route。这个默认的route将url的第一个段映射为一个controller名称,url的第二个段映射为一个controller  action,第三个段映射为命名为id的参数。
       假如,你在网页浏览器的地址栏中键入下面的url:/Home/Index/3,这个默认的route将这个url映射为下面的参数:
controller = Home     controller名称

action = Index          controller  action

id = 3                       id的参数

       当你请求/Home/Index/3这样的url,下面的代码将执行。HomeController.Index(3)

       这个默认的route包含3个默认的参数。如果你没有提供一个 controller,那么 controller默认为Home。同样,action默认为Index,id参数默认为空字符串。
       让我们来看一些关于默认的route怎么映射urls为controller  actions的例子。假如你在你的浏览器地址栏中输入如下的url:/Home, 由于这些默认的route参数有一些相关的默认值,键入这样的URL,将导致HomeController类的Index()方法(如Listing 2)被调用。

 

 

 1 namespace MvcRoutingApp.Controllers
 2 {
 3     [HandleError]
 4     public class HomeController : Controller
 5     {
 6         public ActionResult Index(string id)
 7         {
 8             ViewData["Message"= "欢迎使用 ASP.NET MVC!";
 9 
10             return View();
11         }
12 
13         public ActionResult About()
14         {
15             return View();
16         }
17     }
18 }
19 
20 

 

       

         在Listing 2中,这个HomeController 类包含一个名为Index()的方法。这个URL /Home导致Index()方法被调用,一个空的字符串将作为id参数的值。由于mvc框架调用controller actions的这种方式,这个URL /Home同样匹配HomeController类中的Index()方法(如Listing 3)。
Listing 3 - HomeController.cs (Index action with no parameter)

    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }

        在Listing 3中,这个Index()方法不接收任何参数。这个URL /Home将导致Index()方法被调用。URL /Home/Index/3同样调用这个方法(ID被忽略)。


Listing 4 - HomeController.cs (Index action with nullable parameter)


    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index(int? id)
        {
            return View();
        }
    }

       在Listing 4中, Index() 方法有一个整数参数. 由于这个参数是可空参数 ,  Index() 将被调用而不引起错误.

       最后, 使用 URL /Home 来调用如Listing 5中的Index() 方法 将导致异常,因为这个ID参数不是一个可空的参数。如果你试图去调用这个Index()方法,你将获得如下图所示的错误。

Listing 5 - HomeController.cs (Index action with Id parameter)
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index(int id)
        {
            return View();
        }
    }

      另一方面,使用如Listing 5中的Index controller action,URL /Home/Index/3运行正常。Index controller action in Listing 5. /Home/Index/3请求将导致Index()方法被调用,ID参数拥有一个3的值。

 

总结


      这是一个关于ASP.NET Routing的简要介绍. 应该了解了这个默认的route如何将URLs映射为controller actions。


创建自定义的Routes (C#)

         这个教程,你将学会怎样添加一个自定义的route到一个asp.net mvc应用程序。你将学会在Global.asax文件中,怎样使用一个自定义的route来修改这个默认的route table。
      对于许多简单的ASP.NET MVC 应用程序,这个默认的route  table将运行得很好。然而,你可能发现,你可能特定的routing 需求。那样的话,你可能需要创建一个自定义的route。
      设想一下,例如,你正在建立一个博客应用程序,你可能想要去处理像/Archive/12-25-2009的输入请求。


      当一个用户键入这个请求,你想要返回与日期为12/25/2009相符的博客实体。为了处理这种类型的请求,你需要去创建一个自定义的route。
      在 Listing 1中,这个Global.asax文件中包含一个新的名为Blog的自定义route,它处理类似于/Archive/entry date的请求。
Listing 1 - Global.asax (with custom route)

    

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.Mvc;
 6 using System.Web.Routing;
 7 
 8 namespace MvcRoutingApp
 9 {
10     // 注意: 有关启用 IIS6 或 IIS7 经典模式的说明,
11     // 请访问 http://go.microsoft.com/?LinkId=9394801
12 
13     public class MvcApplication : System.Web.HttpApplication
14     {
15         public static void RegisterRoutes(RouteCollection routes)
16         {
17             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
18             routes.MapRoute(
19                 "Blog"// 路由名称
20                 "Archive/{entryDate}/{id}"// 带有参数的 URL
21                 new { controller = "Archive", action = "Entry", id = UrlParameter.Optional } // 参数默认值
22             );
23             routes.MapRoute(
24                 "Default"// 路由名称
25                 "{controller}/{action}/{id}"// 带有参数的 URL
26                 new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值
27             );
28 
29         }
30 
31         protected void Application_Start()
32         {
33             AreaRegistration.RegisterAllAreas();
34 
35             RegisterRoutes(RouteTable.Routes);
36         }
37     }
38 }

 

 

        你添加到route  table的routes的顺序是很重要的。我们新自定义的blog  route在现存的默认route之前添加。如果你颠倒了顺序,那么这个默认的route总是先调用而不是这个自定义的route。
        这个自定义的blog  toute匹配任何以 /Archive/ 开头的请求。所以,它匹配所有下列URLs:
/Archive/12-25-2009

/Archive/10-6-2004

/Archive/apple

        这个自定义的route将输入的请求映射至名为Archive的controller,并且调用 Entry() action。当 Entry() action被调用的时候,这个输入的日期被当作名为entryDate的参数。
Listing 2 - ArchiveController.cs
    public class ArchiveController : Controller
    {

        public string Entry(DateTime entryDate)
        {
            return "You requested the date:" + entryDate.ToString();
        }

    }

      注意,在Listing 2中这个Entry()方法接收一个类型为DateTime的参数。MVC框架是足够智能的,它自动将URL中输入的date转换为一个DateTime值。如果URL中输入的date不能转换为DateTime,错误将被引发。

 

总结
      这个教程演示怎样来创建一个自定义的route。你学会了怎样在Global.asax 文件中添加一个自定义的route到route  table。我们讨论了怎样为blog实体将请求映射为名为ArchiveController的controller,名为Entry()的controller action。

 

 

posted @ 2010-08-25 22:38  jasen.kin  阅读(6456)  评论(5编辑  收藏  举报