软件开发技术交流

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

在这篇教程里,将向你介绍每个ASP.NET MVC程序都有的重要特性,叫做 ASP.NET Routing。 ASP.NET Routing 模块负责将传入的浏览器请求映射到特定的MVC控制器actions。教程最后,你会理解标准路由表是如何将请求映射到控制器action的。

使用默认路由表
新建 ASP.NET MVC 程序,它已经配置好使用 ASP.NET Routing了。 ASP.NET Routing 在两个地方被创建。

第一个, ASP.NET Routing 在程序的Web配置文件(Web.config 文件)中被启用。配置文件中有4个与路由相对的节点:system.web.httpModules 节, system.web.httpHandlers 节, system.webserver.modules 节, 和 system.webserver.handlers 节。 小心不要删除这些节点因为没有这些节点路由就不能再工作了。

第二个, 也是最重要的一个,路由表在程序的 Global.asax 文件中被创建。 Global.asax 文件时一个特殊的文件,它包括ASP.NET 程序生命周期事件的处理程序。路由表在Application Start 事件之中被创建。

代码1中包含了ASP.NET MVC程序的默认 Global.asax 文件。

代码1 – Global.asax.cs

view plaincopy to clipboardprint?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);           }       }   }  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);
        }
    }
}
 
当 MVC 程序首次启动时, Application_Start() 方法被调用。相反,这个方法调用 RegisterRoutes() 方法。 RegisterRoutes() 方法创建路由表。

默认路由表包含一个简单的路由(名为Default)。默认路由将URL的第一段映射到控制器名称,第二段映射到控制器action,第三段映射到名为 id 的参数。

想象你输入以下URL到你的浏览器地址栏中:

/Home/Index/3

默认路由将此URL映射到以下参数:

controller = Home

action = Index

id = 3

当你请求此 URL /Home/Index/3, 以下代码将被执行:

HomeController.Index(3)

默认路由度这三个参数都包括了默认值。如果你不提供controller,那么controller参数默认为 Home。 如果你不提供action,那么action默认为 Index。 最后,如果你不提供id, id参数默认为空字符串。

让我们来看看一些默认路由如何映射URL到控制器action的例子。想象你输入以下URL到你的浏览器地址栏中:

/Home

由于默认路由的默认值,这个 URL 会调用 HomeController类的 Index() ,如代码2所示。

代码2 – HomeController.cs

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

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index(string id)
        {
            return View();
        }
    }
}
 
在代码2中,HomeController 类包括一个名为 Index() 的方法,它接受一个名为Id的简单参数。 URL /Home 使得 Index() 方法在Id参数的值为空字符串的情况下被调用。

由于MVC框架调用控制器actions的方式,URL /Home 也匹配代码3中HomeController类的 Index() 方法。

代码3 – HomeController.cs (无参Index action)

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

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }
    }
}
 
代码3中的 Index() 方法不接受任何参数。 URL /Home 会使得 Index() 方法被调用。 URL /Home/Index/3 也会调用这个方法(Id 被忽略)。

URL /Home 还匹配代码4中HomeController类的 Index() 方法。

代码 4 – HomeController.cs (带有可空类型参数的Index action)

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

namespace MvcApplication1.Controllers
{
    [HandleError]
    public class HomeController : Controller
    {
        public ActionResult Index(int? id)
        {
            return View();
        }
    }
}
 
在代码4中, Index() 方法具有一个整形参数。由于该参数为可空参数(值可以为null),Index() 方法可以被调用而不产生任何错误。

最后,用URL /Home来调用代码5中的 Index() 方法会导致异常,因为参数不是一个可空参数。如果你试图调用Index()方法那么就会得到图1所示的错误。

代码5 – HomeController.cs (带有Id参数的Index action)

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

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

 
图01: 调用带参数的控制器 action (点击查看完整大小)

另一方面,URL /Home/Index/3 在代码5中的Index控制器中就会运行良好。请求 /Home/Index/3 使得 Index() 方法带上值为3的Id参数被调用。

总结
这篇教程的母的是要向你简单介绍 ASP.NET Routing。我们考查了由ASP.NET MVC程序得到的默认路由表。你学到了默认路由如何将URL映射到控制actions。

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ojlovecd/archive/2010/02/06/5294330.aspx

posted on 2010-04-13 13:58  Felix888  阅读(295)  评论(0)    收藏  举报