/// <summary> /// 根据UrlActionInfo收集到的命名空间及类名计算ServiceController的全名 /// 如果希望在URL中使用简短或者缩写的命名空间,可以重写这个方法 /// </summary> /// <param name="info">UrlActionInfo的实例</param> /// <returns></returns> public virtual string GetServiceFullName(UrlActionInfo info) { // 扩展点:允许自定义判断ServiceController的实现方式 return(info.Namesapce + (string.IsNullOrEmpty(info.Namesapce) ? string.Empty : ".") + EnsureServicePostfix(info.ClassName)); }
/// <summary> /// 根据一个Action的调用信息(类名与方法名),返回内部表示的调用信息。 /// </summary> /// <param name="info"></param> /// <returns></returns> public InvokeInfo GetActionInvokeInfo(UrlActionInfo info) { if (info == null) { throw new ArgumentNullException("info"); } HttpRequest request = _context.Request; if (info.Action == null) { info.Action = info.MethodName; } if (info.Controller == null) { info.Namesapce = GetNamespaceMap(info.Namesapce); info.Controller = s_recognizer.GetServiceFullName(info); } InvokeInfo vkInfo = new InvokeInfo(); vkInfo.Controller = info.ControllerDescription ?? GetServiceController(info.Controller); if (vkInfo.Controller == null) { return(null); } vkInfo.Action = info.ActionDescription ?? GetServiceAction(vkInfo.Controller.ControllerType, info.Action); if (vkInfo.Action == null) { return(null); } if (vkInfo.Action.MethodInfo.IsStatic) { throw new NotSupportedException("不支持静态的Action方法。"); } vkInfo.Instance = ControllerFactory.GetController(vkInfo.Controller.ControllerType); vkInfo.UrlActionInfo = info; return(vkInfo); }
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); }
/// <summary> /// 根据UrlActionInfo收集到的命名空间及类名计算ServiceController的全名 /// 如果希望在URL中使用简短或者缩写的命名空间,可以重写这个方法 /// </summary> /// <param name="info">UrlActionInfo的实例</param> /// <returns></returns> public virtual string GetServiceFullName(UrlActionInfo info) { return(info.Namesapce + (string.IsNullOrEmpty(info.Namesapce) ? string.Empty : ".") + EnsureServicePostfix(info.ClassName)); }
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.IsSame("namespace") || kvp.Key.IsSame("controller") || kvp.Key.IsSame("action") ) continue; string value = kvp.Value as string; if( string.IsNullOrEmpty(value) == false ) info.AddParam(kvp.Key, value); } return info; }
/// <summary> /// 根据UrlActionInfo收集到的命名空间及类名计算ServiceController的全名 /// 如果希望在URL中使用简短或者缩写的命名空间,可以重写这个方法 /// </summary> /// <param name="info">UrlActionInfo的实例</param> /// <returns></returns> public virtual string GetServiceFullName(UrlActionInfo info) { return info.Namesapce + (string.IsNullOrEmpty(info.Namesapce) ? string.Empty : ".") + EnsureServicePostfix(info.ClassName); }