标签:
这段时间在读园子里Artech大神的《ASP.NET MVC5框架揭秘》,慢慢地从底层了解了MVC模式的设计思路。下面是一些阅读的总结。
传统的Web Forms应用,URL指向的是具体的物理文件,而ASP.NET MVC应用一般指向的是某个Controller中的某个Action方法。URL与目标Controller/Action之间的映射关系是通过“路由”来实现的。
路由系统中的几个核心类的描述:
RouteBase
public abstract class RouteBase {
//.NET Framwork4.5以下无该属性(是否对物理文件采取路由) public bool RouteExistingFiles { get; set; }
//获取路由数据 public abstract RouteData GetRouteData(HttpContextBase httpContext);
//路由解析生成一个完整的路径 public abstract VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values); }
RouteData
public class RouteData { public RouteData(); public RouteData(RouteBase route, IRouteHandler routeHandler); public string GetRequiredString(string valueName); public RouteBase Route { get; set; } public IRouteHandler RouteHandler { get; set; } public RouteValueDictionary DataTokens { get; } public RouteValueDictionary Values { get; } }
VirtualPathData
public class VirtualPathData { public VirtualPathData(RouteBase route, string virtualPath); public RouteValueDictionary DataTokens { get; } public RouteBase Route { get; set; } public string VirtualPath { get; set; } }
Route
public class Route : RouteBase { public Route(string url, IRouteHandler routeHandler); public Route(string url, RouteValueDictionary defaults, IRouteHandler routeHandler); public Route(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, IRouteHandler routeHandler); public Route(string url, RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, IRouteHandler routeHandler); //约束(可用正则) public RouteValueDictionary Constraints { get; set; }
//存储额外的变量,不参与路由解析 public RouteValueDictionary DataTokens { get; set; } public RouteValueDictionary Defaults { get; set; } public IRouteHandler RouteHandler { get; set; } public string Url { get; set; } public override RouteData GetRouteData(HttpContextBase httpContext); public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values); }
RouteTable
public class RouteTable { public static RouteCollection Routes { get; } }
RouteCollection
总结下类之间的关系:
Route对象代表一条实际的路由规则。调用Route对象的两个路由匹配方法时进行路由解析,返回的RouteData或VirtualPathData是对Route的封装。RouteTable存储了Web应用的全局路由信息,即多个Route对象。
路由注册
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); }
public static void RegisterRoutes(RouteCollection routes) {
//默认值 var defaults = new RouteValueDictionary{ {"areacode","010"},{"days",2}};
//正则约束 var constaints = new RouteValueDictionary { {"areacode",@"0\d{2,3}"},{"days",@"[1-3]{1}"}};
//说明 var dataTokens = new RouteValueDictionary{ {"defaultCitr\y","BeiJing"},{"defaultDays",2}}; routes.MapPageRoute("default","{areacode}/{}days","~/weather.aspx" ,false,defaults,constaints,dataTokens); }
正则约束是其中一个比较简单的方法,我们还可以通过自定义约束来实现。实现IRouteConstraint的Match方法。
假设现在我们要通过IRouteConstraint来限制IE浏览器的访问。
public class IERouteConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return !(httpContext.Request.UserAgent.Contains("MSIE")); } }
//在原来的约束基础上修改 var constaints = new RouteValueDictionary { {"areacode",@"0\d{2,3}"},{"days",@"[1-3]{1}"},new IERouteConstraint()};
注:针对自定义路由约束的详细内容,请移步:http://www.cnblogs.com/xfrog/archive/2010/12/19/1910428.html。此例也选自该博文。
从HTTP请求到路由解析
注:本博文大多数内容来自《ASP.NET MVC5框架揭秘》中,只是做内容的梳理和总结。
标签:
原文地址:http://www.cnblogs.com/Sinte-Beuve/p/4232234.html