internal static void ExecuteAction(HttpContext context, InvokeInfo vkInfo) { if (context == null) throw new ArgumentNullException("context"); if (vkInfo == null) throw new ArgumentNullException("vkInfo"); //设置响应头 SetVersionHeader(context); //调用Action方法 object result = ExecuteActionInternal(context, vkInfo); if (result != null) { if (result is IActionResult) { //返回视图模型 IActionResult executeResult = result as IActionResult; executeResult.Output(context); } else { //处理方法返回结果 context.Response.ContentType = "text/plain"; context.Response.Write(result.ToString()); } } }
/// <summary> /// 获取请求处理 /// </summary> /// <param name="context"></param> /// <param name="requestType"></param> /// <param name="url"></param> /// <param name="pathTranslated"></param> /// <returns></returns> public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated) { //验证路径是否为请求普通资源 if (url.EndsWith(".aspx")) { PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true); IHttpHandler handler = factory.GetHandler(context, requestType, url, pathTranslated); return(handler); } else { string requestPath = context.Request.Path; //请求路径 string vPath = UrlHelper.GetRealVirtualPath(context); //去除虚拟目录后得到的请求路径 //尝试根据请求路径获取Action InvokeInfo vkInfo = InitEngine.GetInvokeInfo(vPath); if (vkInfo == null) { ExceptionHelper.Throw404Exception(context); } if (vkInfo.Action.Attr != null && !vkInfo.Action.Attr.AllowExecute(context.Request.HttpMethod)) //限定谓词 { ExceptionHelper.Throw403Exception(context); } return(ActionHandler.CreateHandler(vkInfo)); } }
public void ProcessRequest(HttpContext context) { //验证路径是否为请求普通资源 if (context.Request.RawUrl.EndsWith(".aspx")) { PageHandlerFactory factory = (PageHandlerFactory)Activator.CreateInstance(typeof(PageHandlerFactory), true); IHttpHandler handler = factory.GetHandler(context, context.Request.RequestType, context.Request.Url.AbsolutePath, context.Request.PhysicalApplicationPath); handler.ProcessRequest(context); } string requestPath = context.Request.Path; //请求路径 string vPath = UrlHelper.GetRealVirtualPath(context); //去除虚拟目录后得到的请求路径 //尝试根据请求路径获取Action InvokeInfo vkInfo = InitEngine.GetInvokeInfo(vPath); if (vkInfo == null) { HttpContext.Current.Response.StatusCode = 404; HttpContext.Current.Response.Write("无法找到页面:" + context.Request.RawUrl); } else { string code = ValidateProcess(context, vkInfo); switch (code) { case "200": //执行aop if (vkInfo.Controller.Injector != null) { vkInfo.Controller.ControllerContext = context; vkInfo.Controller.Injector.OnControllerExecuting(vkInfo.Controller); } ActionHandler.CreateHandler(vkInfo).ProcessRequest(context); break; case "403": HttpContext.Current.Response.StatusCode = 403; HttpContext.Current.Response.Write("权限不足"); break; case "404": HttpContext.Current.Response.StatusCode = 404; HttpContext.Current.Response.Write("无法找到页面:" + context.Request.RawUrl); break; default: break; } } }
internal static void ExecuteAction(HttpContext context, InvokeInfo vkInfo) { if (context == null) { throw new ArgumentNullException("context"); } if (vkInfo == null) { throw new ArgumentNullException("vkInfo"); } if (vkInfo.Action.Injector != null) { //AOP执行 vkInfo.Action.Injector.OnActionExecuting(vkInfo.Action); } //设置响应头 SetVersionHeader(context); //调用Action方法 object result = ExecuteActionInternal(context, vkInfo); if (result != null) { if (result is IActionResult) { //返回视图模型 IActionResult executeResult = result as IActionResult; executeResult.Output(context); } else { //处理方法返回结果 context.Response.ContentType = "text/plain"; context.Response.Write(result.ToString()); } } if (vkInfo.Action.Injector != null) { //AOP执行 vkInfo.Action.Injector.OnActionExecuted(vkInfo.Action); } if (vkInfo.Controller.Injector != null) { //AOP执行 vkInfo.Controller.Injector.OnControllerExecuted(vkInfo.Controller); } }
internal static object ExecuteActionInternal(HttpContext context, InvokeInfo vkInfo) { if (vkInfo.Instance is BaseController) { PropertyInfo propertyInfo = vkInfo.Instance.GetType().GetProperty("HttpContext"); propertyInfo.SetValue(vkInfo.Instance, context, null); } //准备要传给调用方法的参数 object[] parameters = GetActionCallParameters(context, vkInfo.Action); //调用方法 if (vkInfo.Action.HasReturn) { return(vkInfo.Action.MethodInfo.Invoke(vkInfo.Instance, parameters)); } else { vkInfo.Action.MethodInfo.Invoke(vkInfo.Instance, parameters); return(null); } }
internal static object ExecuteActionInternal(HttpContext context, InvokeInfo vkInfo) { if (vkInfo.Instance is BaseController) { PropertyInfo propertyInfo = vkInfo.Instance.GetType().GetProperty("HttpContext"); propertyInfo.SetValue(vkInfo.Instance, context, null); } //准备要传给调用方法的参数 object[] parameters = GetActionCallParameters(context, vkInfo.Action); //调用方法 if (vkInfo.Action.HasReturn) { return vkInfo.Action.MethodInfo.Invoke(vkInfo.Instance, parameters); } else { vkInfo.Action.MethodInfo.Invoke(vkInfo.Instance, parameters); return null; } }
/// <summary> /// 根据URL获取 调用信息 /// </summary> /// <param name="url"></param> /// <returns></returns> public static InvokeInfo GetInvokeInfo(string url) { if (string.IsNullOrEmpty(url)) { throw new ArgumentNullException("url"); } if (url == "/") { if (RouteTable.Routes["Default"] != null) { //string a = ((Route)(RouteTable.Routes["Default"])); //url = url + ((Route)(RouteTable.Routes["Default"])).Defaults.Values["0"].ToString() + "/" + ((Route)(RouteTable.Routes["Default"])).Defaults.Values[1]; } } url = url.StartsWith("/") ? url.Substring(1) : url; if (url.Contains('.')) { url = url.Substring(0, url.IndexOf(".")); } string[] controllerActionPair = url.Split('/'); string controllerName = (controllerActionPair[0] + "Controller").ToLower(); string actionName = (controllerActionPair[1] + "Action").ToLower(); if (s_ControllerActionDict.ContainsKey(controllerName + "_" + actionName)) { ActionDescription action = s_ControllerActionDict[controllerName + "_" + actionName]; InvokeInfo vkInfo = new InvokeInfo(); vkInfo.Controller = action.PageController; vkInfo.Action = action; vkInfo.Instance = vkInfo.Controller.ControllerType.FastNew(); return(vkInfo); } else { return(null); } }
public static ActionHandler CreateHandler(InvokeInfo vkInfo) { return(new ActionHandler { InvokeInfo = vkInfo }); }
/// <summary> /// 验证请求 /// </summary> /// <param name="context"></param> /// <param name="vkInfo"></param> private string ValidateProcess(HttpContext context, InvokeInfo vkInfo) { string result = "200";//正常请求 if (vkInfo == null) result = "404"; //ExceptionHelper.Throw404Exception(context); if (vkInfo.Controller.AllowRole != null) { if (context.Request.Cookies[PubConst.Client_Unique_ID] != null) { //校验用户身份 string smartIdentity = context.Request.Cookies[PubConst.Client_Unique_ID].Value; smartIdentity = EncryptHelper.DESDeCode(smartIdentity); if (CacheHelper<object>.GetInstance().ContainsKey(smartIdentity)) { object val = CacheHelper<object>.GetInstance().Get(smartIdentity); if (!vkInfo.Controller.AllowRole.AllowAccess(val)) { //无权 //ExceptionHelper.Throw403Exception(context); result = "403"; } } else { //无权 result = "403"; //ExceptionHelper.Throw403Exception(context); } } else { //没有权限 result = "403"; //ExceptionHelper.Throw403Exception(context); } } if (vkInfo.Controller.AllowUser != null) { if (context.Request.Cookies[PubConst.Client_Unique_ID] != null) { //校验用户身份 string smartIdentity = context.Request.Cookies[PubConst.Client_Unique_ID].Value; smartIdentity = EncryptHelper.DESDeCode(smartIdentity); if (CacheHelper<object>.GetInstance().ContainsKey(smartIdentity)) { object val = CacheHelper<object>.GetInstance().Get(smartIdentity); if (!vkInfo.Controller.AllowUser.AllowAccess(val)) { //无权 //ExceptionHelper.Throw403Exception(context); result = "403"; } } else { //无权 //ExceptionHelper.Throw403Exception(context); result = "403"; } } else { //没有权限 //ExceptionHelper.Throw403Exception(context); result = "403"; } } if (vkInfo.Action.Attr != null && !vkInfo.Action.Attr.AllowExecute(context.Request.HttpMethod)) //限定谓词 result = "403"; //ExceptionHelper.Throw403Exception(context); return result; /* if (vkInfo.Action.AllowRole != null) { if (context.Session != null && context.Session["SmartMVC_Current_UserRole"] != null) { if (!vkInfo.Action.AllowRole.AllowAccess(context.Session["SmartMVC_Current_UserRole"])) ExceptionHelper.Throw403Exception(context); } else { ExceptionHelper.Throw403Exception(context); } } if (vkInfo.Action.AllowUser != null) { if (context.Session != null && context.Session["SmartMVC_Current_UserIdentity"] != null) { if (!vkInfo.Action.AllowUser.AllowAccess(context.Session["SmartMVC_Current_UserIdentity"])) ExceptionHelper.Throw403Exception(context); } else { ExceptionHelper.Throw403Exception(context); } } * */ }
/// <summary> /// 验证请求 /// </summary> /// <param name="context"></param> /// <param name="vkInfo"></param> private string ValidateProcess(HttpContext context, InvokeInfo vkInfo) { string result = "200";//正常请求 if (vkInfo == null) { result = "404"; } //ExceptionHelper.Throw404Exception(context); if (vkInfo.Controller.AllowRole != null) { if (context.Request.Cookies[PubConst.Client_Unique_ID] != null) { //校验用户身份 string smartIdentity = context.Request.Cookies[PubConst.Client_Unique_ID].Value; smartIdentity = EncryptHelper.DESDeCode(smartIdentity); if (CacheHelper <object> .GetInstance().ContainsKey(smartIdentity)) { object val = CacheHelper <object> .GetInstance().Get(smartIdentity); if (!vkInfo.Controller.AllowRole.AllowAccess(val)) { //无权 //ExceptionHelper.Throw403Exception(context); result = "403"; } } else { //无权 result = "403"; //ExceptionHelper.Throw403Exception(context); } } else { //没有权限 result = "403"; //ExceptionHelper.Throw403Exception(context); } } if (vkInfo.Controller.AllowUser != null) { if (context.Request.Cookies[PubConst.Client_Unique_ID] != null) { //校验用户身份 string smartIdentity = context.Request.Cookies[PubConst.Client_Unique_ID].Value; smartIdentity = EncryptHelper.DESDeCode(smartIdentity); if (CacheHelper <object> .GetInstance().ContainsKey(smartIdentity)) { object val = CacheHelper <object> .GetInstance().Get(smartIdentity); if (!vkInfo.Controller.AllowUser.AllowAccess(val)) { //无权 //ExceptionHelper.Throw403Exception(context); result = "403"; } } else { //无权 //ExceptionHelper.Throw403Exception(context); result = "403"; } } else { //没有权限 //ExceptionHelper.Throw403Exception(context); result = "403"; } } if (vkInfo.Action.Attr != null && !vkInfo.Action.Attr.AllowExecute(context.Request.HttpMethod)) //限定谓词 { result = "403"; } //ExceptionHelper.Throw403Exception(context); return(result); /* * if (vkInfo.Action.AllowRole != null) * { * if (context.Session != null && context.Session["SmartMVC_Current_UserRole"] != null) * { * if (!vkInfo.Action.AllowRole.AllowAccess(context.Session["SmartMVC_Current_UserRole"])) * ExceptionHelper.Throw403Exception(context); * } * else * { * ExceptionHelper.Throw403Exception(context); * } * } * if (vkInfo.Action.AllowUser != null) * { * if (context.Session != null && context.Session["SmartMVC_Current_UserIdentity"] != null) * { * if (!vkInfo.Action.AllowUser.AllowAccess(context.Session["SmartMVC_Current_UserIdentity"])) * ExceptionHelper.Throw403Exception(context); * } * else * { * ExceptionHelper.Throw403Exception(context); * } * } * */ }
/// <summary> /// 根据URL获取 调用信息 /// </summary> /// <param name="url"></param> /// <returns></returns> public static InvokeInfo GetInvokeInfo(string url) { if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url"); if (url == "/") { if (RouteTable.Routes["Default"] != null) { //string a = ((Route)(RouteTable.Routes["Default"])); //url = url + ((Route)(RouteTable.Routes["Default"])).Defaults.Values["0"].ToString() + "/" + ((Route)(RouteTable.Routes["Default"])).Defaults.Values[1]; } } url = url.StartsWith("/") ? url.Substring(1) : url; if (url.Contains('.')) url = url.Substring(0, url.IndexOf(".")); string[] controllerActionPair = url.Split('/'); string controllerName = (controllerActionPair[0] + "Controller").ToLower(); string actionName = (controllerActionPair[1] + "Action").ToLower(); if (s_ControllerActionDict.ContainsKey(controllerName + "_" + actionName)) { ActionDescription action = s_ControllerActionDict[controllerName + "_" + actionName]; InvokeInfo vkInfo = new InvokeInfo(); vkInfo.Controller = action.PageController; vkInfo.Action = action; vkInfo.Instance = vkInfo.Controller.ControllerType.FastNew(); return vkInfo; } else { return null; } }
public static ActionHandler CreateHandler(InvokeInfo vkInfo) { return new ActionHandler { InvokeInfo = vkInfo }; }