namespace Swift.MVC.Routing { public class SwiftRoute { public SwiftRoute() { } SwiftRoute(string url, Dictionary<string, object> defaultPath, IRouteHandler routeHandler) { TemplateUrl = url; DefaultPath = defaultPath; RouteHandler = routeHandler; } public string TemplateUrl { get; set; } public IRouteHandler RouteHandler { get; set; } public Dictionary<string, object> DefaultPath { get; set; } } }
在SwiftRoutingCollection的Add方法里面,我们需要传入一个SwiftRoute对象,这里的SwiftRoute对象就包含了路由规则的url、默认路由地址、IRouteHandler接口对象三个重要信息,这三个信息都是在Global.asax里面写入的,待会我们测试的再来详细说明。
4、SwiftRouteData.cs代码在上文SwiftRouteCollection的GetRouteData()里面,返回了一个SwiftRouteData对象,这个对象的定义更加简单,仅仅包含两个属性:
namespace Swift.MVC.Routing { public class SwiftRouteData { public IRouteHandler RouteHandler { get; set; } public Dictionary<string, object> RouteValue { get; set; } } }
RouteHandler属性用来保存当前的IRouteHandler对象;
RouteValue属性用来保存当前请求的路由表。
5、IRouteHandler.cs代码上文多次提到了IRouteHandler接口,我们来看看那这个接口内容:
namespace Swift.MVC.Routing { public interface IRouteHandler { System.Web.IHttpHandler GetHttpHandler(SwiftRouteData routeData, HttpContextBase context); } }
这个接口的意义很简单,只有一个方法,用来返回处理当前Http请求的HttpHandler对象。既然这里定义了这个接口,那我们顺便也提一下这个接口的实现类,在当前项目的MVC目录下面,有一个MvcRouteHandler类型:
using Swift.MVC.Routing; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; namespace Swift.MVC { public class MvcRouteHandler:IRouteHandler { 返回处理当前请求的HttpHandler对象 System.Web.IHttpHandler GetHttpHandler(SwiftRouteData routeData, HttpContextBase context) { return new MvcHandler(routeData, context) ; } } }
这个实现类作用更加明确,返回一个具体的HttpHandler对象。
6、UrlRoutingModule.cs代码有了上文的几个类型做支撑,最后我们统筹调度的UrlRoutingModule闪亮登场了。
namespace Swift.MVC.Routing { public class UrlRoutingModule : IHttpModule { #region Property private SwiftRouteCollection _swiftRouteCollection; [System.Diagnostics.CodeAnalysis.SuppressMessage(, , Justification = )] public SwiftRouteCollection SwiftRouteCollection { get { if (_swiftRouteCollection == null) { _swiftRouteCollection = SwiftRouteTable.Routes; } return _swiftRouteCollection; } set { _swiftRouteCollection = value; } } Dispose() { //throw new NotImplementedException(); } public void Init(HttpApplication app) { app.PostResolveRequestCache += app_PostResolveRequestCache; } void app_PostResolveRequestCache(object sender, EventArgs e) { var app = (HttpApplication)sender; contextbase = new HttpContextWrapper(app.Context); PostResolveRequestCache(contextbase); } PostResolveRequestCache(HttpContextBase context) { //1.传入当前上下文对象,得到与当前请求匹配的SwiftRouteData对象 SwiftRouteData routeData = this.SwiftRouteCollection.GetRouteData(context); if (routeData == null) { return; } //2.从SwiftRouteData对象里面得到当前的RouteHandler对象。 IRouteHandler routeHandler = routeData.RouteHandler; if (routeHandler == null) { return; } //3.根据RequestContext对象得到处理当前请求的HttpHandler(MvcHandler)。 IHttpHandler httpHandler = routeHandler.GetHttpHandler(routeData, context); if (httpHandler == null) { return; } //4.请求转到HttpHandler进行处理(进入到ProcessRequest方法)。这一步很重要,由这一步开始,请求才由UrlRoutingModule转到了MvcHandler里面 context.RemapHandler(httpHandler); } } }