一起谈.NET技术,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:


public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // 参数默认值
);

}

protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}

  当一个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)。


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

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


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

  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的请求。


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。

时间: 2024-08-02 11:49:00

一起谈.NET技术,ASP.NET MVC Routing概述的相关文章

ASP.NET MVC Routing概述 C#描述

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.

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.ht

一起谈.NET技术,ASP.NET MVC 3 概述

原文地址:http://www.asp.net/mvc/mvc3 导言 ASP.NET MVC3 在 ASP.NET MVC 1 和 2 的基础上,增加了大量的特性,使得代码更加简化,并且可以深度扩展.这篇文章提供包含在此次发布中的许多新特性的说明,分为以下部分: Razor 视图引擎 支持多视图引擎 Controller 改进 JavaScript 和 Ajax Model 验证的改进 依赖注入 Dependency Injection 的改进 其他新特性 Razor 视图引擎 ASP.NET

ASP.NET MVC 3 概述

原文地址:http://www.asp.net/mvc/mvc3 导言 ASP.NET MVC3 在 ASP.NET MVC 1 和 2 的基础上,增加了大量的特性,使得代码更加简化,并且可以深度扩展.这篇文章提供包含在此次发布中的许多新特性的说明,分为以下部分: Razor 视图引擎 支持多视图引擎 Controller 改进 JavaScript 和 Ajax Model 验证的改进 依赖注入 Dependency Injection 的改进 其他新特性 Razor 视图引擎 ASP.NET

ASP.NET MVC 3 概述“.NET研究”

原文地址:http://www.asp.net/mvc/mvc3 导言 ASP.NET MVC3 在 ASP.NET MVC 1 和 2 的基础上,增加了大量的特性,使得代码更加简化,并且可以深度扩展.这篇文章提供包含在此次发布中的许多新特性的说明,分为以下部分: Razor 视图引擎 支持多视图引擎 Controller 改进 JavaScript 和 Ajax Model 验证的改进 依赖注入 Dependency Injection 的改进 其他新特性 Razor 视图引擎 ASP.NET

一起谈.NET技术,一个MVC分页Helper

本人写的一个分页Helper,支持普通分页(也就是,首页.上一页.下一页.末页等),综合分页(普通分页和数字分页的综合).下面是分页效果: 分页代码: PagerHelper.cs 代码   1 using System;  2  using System.Collections.Generic;  3 using System.Collections.Specialized;  4 using System.Linq;  5 using System.Web;  6 using System.

ASP.NET MVC Routing Debugger路由调试工具

官网地址:http://blog.csdn.net/sgear/article/details/6789882 To  use this, simply download the following zip file and place the assembly inside of it into your bin folder. Then in your Global.asax.cs file add one line to theApplication_Start method (in bo

自学MVC看这里——全网最全ASP.NET MVC 教程汇总

MVC架构已深得人心,微软也不甘落后,推出了Asp.net MVC.小编特意整理博客园乃至整个网络最具价值的MVC技术原创文章,为想要学习ASP.NET MVC技术的学习者提供一个整合学习入口.本文从Why,What,How三个角度整理MVC 的学习资源,让学习者第一时间找到最有价值的文章,获取最彻底的ASp.NET MVC 框架知识,Let's go!   1. Why :为什么需要ASP.NET MVC 本章主要为大家汇总了为什么学习Asp.net MVC替代WebForms,产生ASP.N

学习 ASP.NET MVC (第三回)实战篇

本系列文章导航 学习 ASP.NET MVC (第一回)理论篇 学习 ASP.NET MVC (第二回)实战篇 学习 ASP.NET MVC (第三回)实战篇 学习 ASP.NET MVC (第四回)实战篇 学习 ASP.NET MVC (第五回)理论篇 创建Controllers和Views我们再次回顾下ASP.NET MVC的基本工作流程:URL-->Controller(会执行一个方法Action)-->从数据库中获取一个Model对象-->将Model作为ViewData传递给V