简单的mvc之三:灵活的路由(上)
在上一篇关于管线的随笔中已经提及了管线,通过对管线的分析,我们可以得到下面几个结论:路由系统由URLRoutingModule模块实现,它订阅了PostResolvRequestCache事件;路由系统通过查阅路由并尽可能的通过RemapHandler方法,确定excuteHandler阶段执行的IHttphandler。这一篇随笔想详细谢谢路由的定义、注册和导航的具体过程。
路由系统的导航过程定义于URLRoutingModule,具体实现如下:
public virtual void PostResolveRequestCache(HttpContextBase context)
{
RouteData routeData = this.RouteCollection.GetRouteData(context);
if (routeData != null)
{
IRouteHandler routeHandler = routeData.RouteHandler;
if (routeHandler == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, SR.GetString("UrlRoutingModule_NoRouteHandler"), new object[]));
}
if (!(routeHandler is StopRoutingHandler))
{
RequestContext requestContext = new RequestContext(context, routeData);
context.Request.RequestContext = requestContext;
IHttpHandler httpHandler = routeHandler.GetHttpHandler(requestContext);
if (httpHandler == null)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentUICulture, SR.GetString("UrlRoutingModule_NoHttpHandler"), new object[] { routeHandler.GetType() }));
}
if (httpHandler is UrlAuthFailureHandler)
{
if (!FormsAuthenticationModule.FormsAuthRequired)
{
throw new HttpException(0x191, SR.GetString("Assess_Denied_Description3"));
}
UrlAuthorizationModule.ReportUrlAuthorizationFailure(HttpContext.Current, this);
}
else
{
context.RemapHandler(httpHandler);
}
}
}
}
代码中可以很明显看出导航逻辑:通过全局全局路由表Routeable.Routes.GetRouteData()方法获得RouteData,然后通过RouteData.GetHttphandler()获得IRoutehandler,最后通过HttpContext.RemapHandler()注册IRouteHandler.GetHttpHandler()得到的IhttpHandler。代码中的出现的StopRoutingHandler后面再说。
路由表的切入口在全局静态类RouteTable.Routes,注册的过程放在HttpApplication的Appliation_Start方法中,这个方法如同静态构造函数一般,只启动一次,所以所有需要初始化的全局对象都可以放在这个方法中。vs建立的项目中,Route表的注册被分离出来,放在了App_Start文件夹中,使代码更加清晰。例如以下代码所示,3个路由被注册:
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Self", action = "Index", id = UrlParameter.Optional }
); routes.MapRoute(
name: "CatchAll",
url: "{*input}",
defaults: new { controller = "Home", action = "index" }
);
}
上面的代码中使用的IgnoreRoute()与MapRoute()皆为扩展方法,用来简化路由注册过程,前者初始化一个Route对象,或者初始化一个IgnoreRouteInternal(继承于Route,私有封闭)对象,然后使用RouteTable.Routes.Add()方法添加如注册表。通过如下代码,可以清楚的看到注册的route:
public void Routes() {
foreach (var routeBase in RouteTable.Routes) {
var route = routeBase as Route;
if (route != null) {
Response.Write(String.Format("{1,26} {0}<br/>",
route.GetType().ToString(), route.Url
).Replace(" "," "));
} else {
Response.Write(String.Format("other:{0}<br/>", routeBase.GetType()));
}
}
}
得到的结果如下:
从图中我们可以看到后3个路由正是我们已经注册的3个路由,至于第一个路由,实际上是托管于asp.net的web api应用路由,这个后面再说。同时图中的第二列的Type也佐证了上面所说的不同的Route类说法。
那么Route类的具体实现呢?首先我们看看Route的继承树:
看着很熟悉,因为集成树中的类在上述的路由图中已经全部出现,整个树的基类是RouteBase,也就是图中高亮的那个,Routes中的集合类型也是RouteBase,下面是RouteBase的定义:
[TypeForwardedFrom("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
public abstract class RouteBase
{
// Fields
private bool _routeExistingFiles; // Methods
protected RouteBase();
public abstract RouteData GetRouteData(HttpContextBase httpContext);
public abstract VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values); // Properties
public bool RouteExistingFiles { [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] set; }
}
这个一个抽象类,核心方法就是URLRouting中出现过的GetRouteData()方法,还有一个很重要的属性—RouteExistingFiles,这个属性一般用来实现对.aspx兼容支持。RouteBase最常用也是最重要的子类便是Route,代码如下:
[TypeForwardedFrom("System.Web.Routing, Version=3.5.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
public class Route : RouteBase
{
// Fields
private ParsedRoute _parsedRoute;
private string _url;
private const string HttpMethodParameterName = "httpMethod"; // Methods
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 override RouteData GetRouteData(HttpContextBase httpContext);
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values);
protected virtual bool ProcessConstraint(HttpContextBase httpContext, object constraint, string parameterName, RouteValueDictionary values, RouteDirection routeDirection);
private bool ProcessConstraints(HttpContextBase httpContext, RouteValueDictionary values, RouteDirection routeDirection); // Properties
public RouteValueDictionary Constraints { get; [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] set; }
public RouteValueDictionary DataTokens { [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] set; }
public RouteValueDictionary Defaults { [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] set; }
public IRouteHandler RouteHandler { [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] get; [CompilerGenerated, TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] set; }
public string Url { get; set; }
}
相较于RouteBase,Route有5个属性,其中Url,Constraints用来过滤进入请求进行匹配,RouteHandler用来返回IHttpHandler,DataTokens与Defaults则应用于后面的控制器查找,激活与数据传递。这些就以后在细节补充中说这里就不散开了。
从上面的Route的定义结合最前面说的导航过程,RouteBase->IRouteHandler->IhttpHandler的过程清晰可见。在前面曾经搁置的类StopRoutingHandler就属于IRoutehandler,而这个RouteHandler并不是由Route类获得的,而是前面出现的IgnoreRoute获得的,相关代码:
public IgnoreRouteInternal(string url) : base(url, new StopRoutingHandler())
{
}
到这里IgnoreRoute()的方法的作用就真正明了了,和MapRoute()方法想反,它定义的不是对某一类路径的拦截,而是放弃对某一类路径的拦截,以便留给后面的管线过程处理,这个管线过程就是后面要说的ExcuteHandler过程(内部,没有公开事件),具体到文中注册的路由可以看到它放弃的是对*.axd类地址的拦截。但是注册的好处是直接跳过,提高效率。
本来想一次说完的,写到这里发现还有很多没写到,比如全局类型RouteTable等,那就分上下篇吧。最后留个问题:两个路由,一个的Url定义为{input},一个定义为{*input},二者有什么区别?
简单的mvc之三:灵活的路由(上)的更多相关文章
- 005. Asp.Net Routing与MVC 之三: 路由在MVC的使用
上次讲到请求如何激活Controller和Action,这次讲下MVC中路由的使用.本次两个关注点: 遗留:ModelBinder.BindModel的过程 MVC中路由的使用 MVC 5中的Acti ...
- 编写简单的spring mvc程序,在tomcat上部署
编写简单的spring mvc程序,在tomcat上部署 1 用java 配置spring mvc ,可以省去web.xmlpackage hello;import org.springframewo ...
- ASP.NET Core MVC 中两种路由的简单配置
1.全局约定路由 这种方式配置优先级比较低,如果控制器或者方法上标记了特性路由那么优先走特性路由. 当建立好一个mvc项目里,路由都是默认配置好的. 如果建立的是空项目那么需要手动配置: 1.需要在C ...
- MVC架构模式分析与设计(一)---简单的mvc架构
首先 我要感谢慕课网的老师提供视频资料 http://www.imooc.com/learn/69 下面我来进行笔记 我们制作一个简单的mvc架构 制作第一个控制器 testController.cl ...
- MVC 插件化框架支持原生MVC的Area和路由特性
.NET MVC 插件化框架支持原生MVC的Area和路由特性 前面开放的源码只是简单的Plugin的实现,支持了插件的热插拔,最近晚上偶然想到,原生的MVC提供Areas和RouteAtrribut ...
- 简单的mvc之一:简单的开始
mvc学习到现在,相对所学到的一系列的知识做一个总结,于是就有了这个标题—简单的mvc.文如名,写的是简单的mvc的知识,目标群也不言而喻.这一篇来个简单的开始,从头建立一个web项目,比如hello ...
- [.NET] 一步步打造一个简单的 MVC 网站 - BooksStore(一)
一步步打造一个简单的 MVC 网站 - BooksStore(一) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore 简介 主 ...
- [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(二)
一步步打造一个简单的 MVC 电商网站 - BooksStore(二) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore 前: ...
- [.NET] 一步步打造一个简单的 MVC 电商网站 - BooksStore(一)
一步步打造一个简单的 MVC 电商网站 - BooksStore(一) 本系列的 GitHub地址:https://github.com/liqingwen2015/Wen.BooksStore &l ...
随机推荐
- uva757 - Gone Fishing(馋)
题目:uva757 - Gone Fishing(贪心) 题目大意:有N个湖泊仅仅有一条通路将这些湖泊相连. 每一个湖泊都会给最開始5分钟间隔内能够调到的鱼(f).然后给每过5分钟降低的鱼的数量(d) ...
- Java经典23创意模式设计模式(两)
本文介绍5其余两种创意模式:模型构建器(Builder).原型模型(PROTOTYPE). 一.建造者模式(别名:生成者模式) 将复杂对象的构建和它的表示分离,使得相同的构建过程能够创建不同的表示. ...
- Tomcat剖析(五):Tomcat 容器
Tomcat剖析(五):Tomcat 容器 1. Tomcat剖析(一):一个简单的Web服务器 2. Tomcat剖析(二):一个简单的Servlet服务器 3. Tomcat剖析(三):连接器(1 ...
- linux复制文件命令scp
linux大多数复制我们的递送工具使用,有着ftp,scp等一下. 当中scp命令很easy快捷, 本机到远程:scp (-r) 本地目录或者文件路径 远程ip:目录 远程到本机:scp (-r) 远 ...
- C#创建和初始化类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace C_编辑 ...
- A WebBrowser Toy
原文:A WebBrowser Toy 记得上大学时,某老师为了防止学生上课不听讲,只准学生在课堂上看他放映的PPT,不准学生拷贝,而考试的内容恰恰是PPT上的内容,于是一个同学来找我,我就用VB写了 ...
- MySql绿色版配置及使用详解
原文:MySql绿色版配置及使用详解 最近在做项目开发时用到了MySql数据库,在看了一些有关MySql的文章后,很快就上手使用了.在使用的过程中还是出现了一些问题,因为使用的是绿色免安装版的MySq ...
- The Swift Programming Language-官方教程精译Swift(9) 枚举-- --Enumerations
枚举定义了一个通用类型的一组相关的值,使你可以在你的代码中以一个安全的方式来使用这些值. 如果你熟悉 C 语言,你就会知道,在 C 语言中枚举指定相关名称为一组整型值.Swift 中的枚举更加灵活 ...
- Struts2_2_第一Struts2应用
web.xml文件的配置与1同样. 1)HelloWorld类的代码: public class HelloWorldAction { private String message; public S ...
- MVC验证06-自定义错误信息
原文:MVC验证06-自定义错误信息 本文体验自定义错误信息. 系统默认的错误信息 在"MVC验证02-自定义验证规则.邮件验证"中,我们自定义了一个验证Email的类.如果输 ...