mvc 和webapi的路由都是经过注册到RouteTable.Routes中,而后在urlroutingmodule中路由到对应routehander,那之前webform程序没有注册路由又是怎么找到对应的handler的呢?web
在httpapplication中注册事件中有一个MapHandlerExecutionStepapi
internal override void BuildSteps(WaitCallback stepCallback) { ....... arrayList.Add(new HttpApplication.MapHandlerExecutionStep(application)); ...... }
在这个事件中缓存
void HttpApplication.IExecutionStep.Execute() { ........ context.Handler = this._application.MapHttpHandler(context, request.RequestType, request.FilePathObject, request.PhysicalPathInternal, false); ...... }
// System.Web.HttpApplication internal IHttpHandler MapHttpHandler(HttpContext context, string requestType, VirtualPath path, string pathTranslated, bool useAppConfig) { IHttpHandler httpHandler = (context.ServerExecuteDepth == 0) ? context.RemapHandlerInstance : null; using (new ApplicationImpersonationContext()) { if (httpHandler != null) { return httpHandler; } HttpHandlerAction handlerMapping = this.GetHandlerMapping(context, requestType, path, useAppConfig); ........
//根据请求信息包装对应工厂类并放入缓存中,每次从缓存中获取 IHttpHandlerFactory factory = this.GetFactory(handlerMapping); try {
//获取handler IHttpHandlerFactory2 httpHandlerFactory = factory as IHttpHandlerFactory2; if (httpHandlerFactory != null) { httpHandler = httpHandlerFactory.GetHandler(context, requestType, path, pathTranslated); } else { httpHandler = factory.GetHandler(context, requestType, path.VirtualPathString, pathTranslated); } } ......... } return httpHandler; }
因此每次webform或者handler请求后都会从缓存中获取工厂类而后获取对应的handlermvc
如今webform新建程序都会自动添加像mvc路由同样的路由注册文件app
public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { // 在应用程序启动时运行的代码 RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } }
public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings); } }