您的位置:首页 > 编程语言 > ASP

返璞归真 asp.net mvc (2) - 路由(System.Web.Routing)

2014-06-30 14:54 453 查看
介绍

System.Web.Routing - 此命名空间提供用于 URL 路由的类,通过该类可以使用不映射到物理文件的 URL

IRouteHandler - 路由处理程序接口,自定义的路由处理程序都要实现这个接口
RequestContext - 封装所请求的路由的相关信息和当前的 http 上下文信息
RouteData - 所请求的路由的相关信息
RouteCollection - 路由集合
RouteValueDictionary - 不区分大小写的 key/value 字典表
Route - 路由的相关信息

示例

1、MyHandler.cs





using System;


using System.Collections.Generic;


using System.Linq;


using System.Web;




using System.Web.Routing;




namespace MVC.RoutingDemo


{


/// <summary>


/// 路由处理程序


/// </summary>


public class MyRouteHandler : IRouteHandler


{


/*


* IRouteHandler - 路由处理程序接口,自定义的路由处理程序都要实现这个接口


* 该接口有一个方法 IHttpHandler GetHttpHandler(RequestContext requestContext)


* 此方法需要返回一个处理该路由的 http 处理程序 (HttpHandler)


* RequestContext - 封装所请求的路由的相关信息和当前的 http 上下文信息


* RequestContext.HttpContext - 当前的 http 上下文信息


* RequestContext.RouteData - 所请求的路由的相关信息


*/




public IHttpHandler GetHttpHandler(RequestContext requestContext)


{


return new MyHttpHandler(requestContext);


}


}




/// <summary>


/// 自定义的 http 处理程序,由路由决定使用哪个 http 处理程序


/// </summary>


public class MyHttpHandler : IHttpHandler


{


public RequestContext RequestContext { get; private set; }




public MyHttpHandler(RequestContext context)


{


RequestContext = context;


}




public void ProcessRequest(HttpContext httpContext)


{


/*


* RouteData - 所请求的路由的相关信息


* RouteData.Values - 一个字典表(key - 路由参数;value - 路由值)


* RouteData.DataTokens - 自定义需要传递的数据,也是一个字典表


* RouteData.GetRequiredString(string key) - 获取指定的路由参数所对应的路由值


*/




httpContext.Response.Write("相关参数为:<br />");


foreach (var dic in RequestContext.RouteData.Values)


{


httpContext.Response.Write(dic.Key + " : " + dic.Value + "<br />");


}




httpContext.Response.Write("相关Token为:<br />");


foreach (var dic in RequestContext.RouteData.DataTokens)


{


httpContext.Response.Write(dic.Key + " : " + dic.Value + "<br />");


}


}




public bool IsReusable


{


get { return false; }


}


}




/// <summary>


/// 如果有 n 多的路由处理程序,为了方便调用,减少代码,我们可以将其写成一个 Factory 类,如下:


/// </summary>


public class RouteHandlerFactory : IRouteHandler


{


private string _name;


public RouteHandlerFactory(string name)


{


_name = name;


}




public IHttpHandler GetHttpHandler(RequestContext requestContext)


{


if (_name == "My")


return new MyHttpHandler(requestContext);




return new MyHttpHandler(requestContext);


}


}




/// <summary>


/// 演示不经过路由,根据 web.config 中的 HttpHandler 相关配置,由此 HttpHandler 直接处理相关请球。具体配置见 Global.asax


/// </summary>


public class XXXHttpHandler : IHttpHandler


{


public XXXHttpHandler()


{




}




public void ProcessRequest(HttpContext context)


{


context.Response.Write(context.Request.Url.ToString());


}




public bool IsReusable


{


get { return false; }


}


}


}



2、Web.config





<?xml version="1.0"?>


<configuration>


<system.web>


<httpHandlers>




<!--


配置一个自定义的 HttpHandler,用于处理后缀名为 xxx 的页面


如果不需要将 *.xxx 交给路由处理,而是直接让指定的 HttpHandler 处理,请参看 Global.asax 中的配置


-->


<add verb="*" path="*.xxx" type="MVC.RoutingDemo.XXXHttpHandler" />




</httpHandlers>


<httpModules>




<!--


UrlRoutingModule - 用于匹配 asp.net 应用程序中的路由的 http 请求


该模块找到匹配路由后,会检索 IRouteHandler 对象,以查找该路由,然后从该路由处理程序获取一个 IHttpHandler 对象,并将该对象用作当前请求的 http 处理程序


-->


<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>




</httpModules>


</system.web>


</configuration>






3、Global.asax.cs





using System;


using System.Collections.Generic;


using System.Linq;


using System.Web;


using System.Web.Mvc;


using System.Web.Routing;




namespace MVC


{


// 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)


{


/* 路由方向:自上而下,查找到匹配的就去处理,而不会继续往下做匹配 */




/*


* RouteCollection - 路由集合


* RouteCollection.IgnoreRoute() - 指定的地址不由路由处理


* RouteCollection.Add() - 新增一个路由配置


* RouteCollection.MapRoute() - 映射一个路由(Add() 方法的简化版,内部会自动调用 MvcRouteHandler)


*/




// .asd 文件不走此路由


routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


// .xxx 文件不走此路由


routes.Add(new Route("{resource}.xxx/{*pathInfo}", new StopRoutingHandler()));






/*


* RouteValueDictionary - 不区分大小写的 key/value 字典表


* Route - 路由的相关信息


* Route.Url - 路由的 url 匹配模式,{ } 内的为需要匹配的路由参数名


* Route.Defaults - 路由参数的默认值


* Route.RouteHandler - 路由对应的路由处理程序


* Route.DataTokens - 自定义需要传递的数据


* Route.Constraints - 约束参数值的有效性(可以是正则表达式(不区分大小写),也可以是实现了 IRouteConstraint 的对象)


*/




Route route = new Route(


"Routing/{parent}/{child}",




// RouteValueDictionary - 可以接收一个匿名对象为参数,属性名转换为 key, 属性值转换为 value


new RouteValueDictionary(new { parent = "ria", child = "silverlight", controller = "Product", action = "Index" }),




new RoutingDemo.MyRouteHandler()


);


route.DataTokens = new RouteValueDictionary(new { token1 = "abc", token2 = "xyz" });




// HttpMethodConstraint - 实现了 IRouteConstraint 接口的类,用于约束 http 方法


route.Constraints = new RouteValueDictionary(new { parent = @"^[a-zA-Z]+$", httpMethod = new HttpMethodConstraint("GET") });




// 将路由添加进路由集合,名称可以随便设置,但必须唯一(也可以不设置路由名称)


routes.Add("MyRoutingDemo", route);


// routes.Add(route);






// * - 路由也支持通配符


// {*param} - 代表匹配任意值,其中路由参数名为 param


routes.Add(new Route("Wildcard/{*param}", new RouteValueDictionary(new { controller = "Product", action = "Index" }), new RoutingDemo.MyRouteHandler()));






// MapRoute() 封装了 Add(), MapRoute() 内部会自动调用 MvcRouteHandler


// 其中必须要有 controller 项和 action 项


// 其中设置 namespaces 参数相当于 route.DataTokens["Namespaces"] = namespaces;


routes.MapRoute(


"Products",


"ProductList/{pageIndex}",


new { controller = "Product", action = "Index", pageIndex = 0 }


);




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);




// 调试路由(需要 RouteDebug.dll),调用如下语句后,会在每个页面都显示详细的路由信息


// RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);


}


}


}






// 部署在 iis 应注意


// 配置通配符映射程序,类似如下地址


// C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll


// 不要勾选“确认文件是否存在”
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: