internal IHttpHandler GetHttpHandler(HttpContext context, UrlActionInfo info) { if (context == null) { throw new ArgumentNullException("context"); } if (info == null) { throw new ArgumentNullException("info"); } // 获取内部表示的调用信息 ControllerResolver controllerResolver = new ControllerResolver(context); InvokeInfo vkInfo = controllerResolver.GetActionInvokeInfo(info); if (vkInfo == null) { IHttpHandler handler = Http404DebugModule.TryGetHttp404PageHandler(context); if (handler != null) { return(handler); } else { return(null); } } // 创建能够调用Action的HttpHandler return(ActionHandlerFactory.CreateHandler(vkInfo)); }
void app_PostResolveRequestCache(object sender, EventArgs e) { HttpApplication app = (HttpApplication)sender; HttpContext context = app.Context; // 如果已经被处理过了,就不再处理。 if (context.Handler != null) { return; } string virtualPath = context.GetRealVirtualPath(); // 检查有没有匹配的路由规则 RouteData routeData = GetRoute(context, virtualPath); if (routeData == null) { return; // 没有就忽略,重新交给ASP.NET管线继续处理。 } MvcRouteHandler routeHandler = routeData.RouteHandler as MvcRouteHandler; if (routeHandler == null) { //return; // 忽略就会产生404错误了。因此为了排查方便,抛出异常。 throw new InvalidProgramException("使用ClownFish.Web.MvcRoutingModule时,必须指定MvcRouteHandler"); } UrlActionInfo info = routeHandler.GetUrlActionInfo(routeData, context); info.SetHttpcontext(context); IHttpHandler handler = routeHandler.GetHttpHandler(context, info); if (handler != null) { context.RemapHandler(handler); } else { ExceptionHelper.Throw404Exception(context); } }
/// <summary> /// 实现IHttpHandlerFactory接口,从当前请求获取IHttpHandler /// </summary> /// <param name="context"></param> /// <param name="requestType"></param> /// <param name="virtualPath"></param> /// <param name="physicalPath"></param> /// <returns></returns> public IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string physicalPath) { // 说明:这里不使用virtualPath变量,因为不同的配置,这个变量的值会不一样。 // 例如:/Ajax/*/*.aspx 和 /Ajax/* // 为了映射HTTP处理器,下面直接使用context.Request.Path string vPath = context.GetRealVirtualPath(); // 根据请求路径,定位到要执行的Action UrlActionInfo info = ParseUrl(context, vPath); if (info == null) { IHttpHandler handler = Http404DebugModule.TryGetHttp404PageHandler(context); if (handler != null) { return(handler); } ExceptionHelper.Throw404Exception(context); } info.SetHttpcontext(context); // 获取内部表示的调用信息 ControllerResolver controllerResolver = new ControllerResolver(context); InvokeInfo vkInfo = controllerResolver.GetActionInvokeInfo(info); if (vkInfo == null) { IHttpHandler handler = Http404DebugModule.TryGetHttp404PageHandler(context); if (handler != null) { return(handler); } ExceptionHelper.Throw404Exception(context); } // 创建能够调用Action的HttpHandler return(ActionHandlerFactory.CreateHandler(vkInfo)); }
/// <summary> /// 实现IRouteHandler接口 /// </summary> /// <param name="requestContext"></param> /// <returns></returns> public IHttpHandler GetHttpHandler(RequestContext requestContext) { HttpContext context = requestContext.HttpContext.ApplicationInstance.Context; UrlActionInfo info = GetUrlActionInfo(requestContext.RouteData, context); if (info == null) { IHttpHandler handler = Http404DebugModule.TryGetHttp404PageHandler(context); if (handler != null) { return(handler); } ExceptionHelper.Throw404Exception(context); } info.SetHttpcontext(context); return(GetHttpHandler(context, info)); }
internal UrlActionInfo GetUrlActionInfo(RouteData routeData, HttpContext context) { if (routeData == null) { throw new ArgumentNullException("routeData"); } if (context == null) { throw new ArgumentNullException("context"); } // 采用ASP.NET Routing后,这三个参数都应该可以直接获取到, // 如果URL没有指定,可以通过默认值,或者DataToken指定, // 所以不需要像RestServiceModule那样重新计算 string nspace = GetRouteString(routeData, "namespace"); string className = GetRouteString(routeData, "controller"); string action = GetRouteString(routeData, "action"); if (string.IsNullOrEmpty(className) || string.IsNullOrEmpty(action)) { DiagnoseResult diagnoseResult = Http404DebugModule.TryGetDiagnoseResult(context); if (diagnoseResult != null) { diagnoseResult.ErrorMessages.Add("不能从URL中提取到controller和action信息"); } return(null); } if (action == "{HttpMethod}") // 允许定义这个特殊变量 { action = context.Request.HttpMethod; } ControllerResolver controllerResolver = new ControllerResolver(context); UrlActionInfo info = new UrlActionInfo(); info.RoutePattern = (routeData.Route as Route).Url; // 转换失败?? info.Namesapce = controllerResolver.GetNamespaceMap(nspace); info.ClassName = className; info.MethodName = action; info.Action = action; info.Controller = s_recognizer.GetServiceFullName(info); // 将路由提取到的其它URL参数,保存到UrlActionInfo实例中。 foreach (KeyValuePair <string, object> kvp in routeData.Values) { // 排除3个特定名字。 if (kvp.Key.EqualsIgnoreCase("namespace") || kvp.Key.EqualsIgnoreCase("controller") || kvp.Key.EqualsIgnoreCase("action")) { continue; } string value = kvp.Value as string; if (string.IsNullOrEmpty(value) == false) { info.AddParam(kvp.Key, value); } } return(info); }