/// <summary> /// 通过路径注册插件(不推荐使用) /// </summary> /// <param name="pluginFullPath">插件全路径</param> /// <param name="classFullName">类全名</param> /// <param name="controllerName">控制器名字</param> public static void MapRoutePlugin(string pluginFullPath, string classFullName, string controllerName) { lock (_ROUTE_LOCK) { ControllerAssmeblyUtil.RegisterByAssmblyPath(pluginFullPath); URL_CONTROLLER_NAME_ROUTE_MAP[controllerName] = classFullName; URL_CONTROLLER_NAME_ROUTE_MAP_[classFullName] = controllerName; } }
/// <summary> /// 注册二级路由规则(建议不用再此方法) /// </summary> /// <typeparam name="T">控制器类</typeparam> /// <param name="userDefineName">控制器名</param> public static void MapRoute <T>(string userDefineName) where T : BaseController { string classFullName = typeof(T).FullName; lock (_ROUTE_LOCK) { URL_CONTROLLER_NAME_ROUTE_MAP[userDefineName] = classFullName; URL_CONTROLLER_NAME_ROUTE_MAP_[classFullName] = userDefineName; } ControllerAssmeblyUtil.Register <T>(); }
/// <summary> /// 手动注册一个控制器为三级路由规则 /// </summary> /// <typeparam name="T">控制器类</typeparam> /// <param name="areaName">三级路由的名字</param> /// <param name="controllerName">自定义控制器名</param> public static void SetArea <T>(string areaName, string controllerName) where T : BaseController { string controllerName2 = areaName + "/" + controllerName; string classFullName = typeof(T).FullName; lock (_ROUTE_LOCK) { URL_CONTROLLER_NAME_ROUTE_MAP[controllerName2] = classFullName; URL_CONTROLLER_NAME_ROUTE_MAP_[classFullName] = controllerName2; } ControllerAssmeblyUtil.Register <T>(); }
/// <summary> /// 注册二级路由规则(建议不用再此方法) /// </summary> /// <typeparam name="T">控制器类</typeparam> public static void MapRoute <T>() where T : BaseController { string classFullName = typeof(T).FullName; string userDefineName = null; int index = classFullName.LastIndexOf('.'); userDefineName = classFullName.Substring(index + 1); userDefineName = userDefineName.Replace("Controller", ""); lock (_ROUTE_LOCK) { URL_CONTROLLER_NAME_ROUTE_MAP[userDefineName] = classFullName; URL_CONTROLLER_NAME_ROUTE_MAP_[classFullName] = userDefineName; } ControllerAssmeblyUtil.Register <T>(); }
public static object Invoke(string classFullName, string methodName, object[] paramters) { string key = classFullName + methodName; FastInvokeHandler handler = null; bool existKey = true; lock (_keyFastInvokeHandlerLock){ if (_keyFastInvokeHandler.ContainsKey(key)) { handler = _keyFastInvokeHandler[key]; } else { existKey = false; } } if (existKey == false) { Type type = null; bool existTheType = false; lock (_classFullNameTypeDicLock){ existTheType = _classFullNameTypeDic.ContainsKey(classFullName); if (existTheType) { type = _classFullNameTypeDic[classFullName]; } } if (existTheType == false) { type = ControllerAssmeblyUtil.CreateType(classFullName); lock (_classFullNameTypeDicLock){ _classFullNameTypeDic[classFullName] = type; } } var methodInfo = type.GetMethod(methodName); if (methodInfo == null) { return(null); } handler = GetMethodInvoker(methodInfo); lock (_keyFastInvokeHandlerLock){ _keyFastInvokeHandler[key] = handler; } } var obj = ControllerAssmeblyUtil.CreateInstance(classFullName); return(handler.Invoke(obj, paramters)); }
/// <summary> /// 注册指定路径的插件 /// </summary> /// <param name="pluginFullPath">插件全路径</param> public static void MapRoutePlugin(string pluginFullPath) { if (File.Exists(pluginFullPath) == false) { throw new FileNotFoundException("指定路径的插件文件不存在,请查看此路径:" + pluginFullPath); } Moon.Orm.Util.LogUtil.Debug("发现插件,PluginFullPath:" + pluginFullPath); var assmebly = ControllerAssmeblyUtil.RegisterByAssmblyPath(pluginFullPath); var allTypes = assmebly.GetTypes(); foreach (var type in allTypes) { if (type.IsClass && type.IsSubclassOf(typeof(BaseController))) { string classFullName = type.FullName; Moon.Orm.Util.LogUtil.Debug("载入插件Controller:" + classFullName); int index = classFullName.LastIndexOf('.'); string userDefineName = null; userDefineName = classFullName.Substring(index + 1); userDefineName = userDefineName.Replace("Controller", "");//去掉Controller得到对应的名字 bool existArea; var areaName = AreaUtil.GetControllerAreaName(type, out existArea); if (existArea) { userDefineName = areaName + "/" + userDefineName; } lock (_ROUTE_LOCK) { URL_CONTROLLER_NAME_ROUTE_MAP[userDefineName] = classFullName; URL_CONTROLLER_NAME_ROUTE_MAP_[classFullName] = userDefineName; } } } lock (_ROUTE_LOCK) { ControllerAssmeblyUtil.RegisterByAssmblyPath(pluginFullPath); } }
/// <summary> /// moon所有的请求 /// </summary> /// <param name="classFullName">类的完全限定名</param> /// <param name="methodName">方法名(只能是public)</param> /// <param name="context">当前的HttpContext</param> public static void Process(string classFullName, string methodName, HttpContext context) { var classType = ControllerAssmeblyUtil.CreateType(classFullName); var classInstance = ControllerAssmeblyUtil.CreateInstance(classFullName); if (classInstance == null) { Moon.Orm.Util.LogUtil.Error("classInstance null,classFullName:" + classFullName + " " + context.Request.RawUrl); var err = "current request doesn't exist"; throw new Exception(err); } if (classType == null) { var err = "current request doesn't exist"; throw new Exception(err); } var methodInfo = classType.GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance); if (methodInfo == null) { return; } //================= BaseController baseControllerInstance = null; Dictionary <string, object> viewData = new Dictionary <string, object>(); if (classInstance is BaseController) { BaseController obj = classInstance as BaseController; obj.CurrentHttpContext = context; viewData = obj.ViewData; baseControllerInstance = obj; } else { throw new Exception("请求的类没有继承BaseController"); } var atts = methodInfo.GetCustomAttributes(typeof(ResultAttribute), true); ResultAttribute resultAttr = null; if (atts.Length > 0) { resultAttr = atts[0] as ResultAttribute; } else { resultAttr = new TemplateResultAttribute();//默认为模板类型 } //------------ object resultObj = null; //------------ var customAspectAttributes = methodInfo.GetCustomAttributes(typeof(AspectAttribute), true); List <ValidationResult> resultList = new List <ValidationResult>(); ErrorResultReturnType fluentValidateResultType = ErrorResultReturnType.Json; bool dontValidateRequest = ExistAttribute <DontValidateRequestAttribute>(methodInfo); int methodRequestType = GetMethodSupportRequestType(methodInfo); if (context.Request.RequestType == "GET") { if (methodRequestType == (int)RequestType.POST) { var err = "Current request is GET,but you didn't set a GET attribute to your method :" + classFullName + "->" + methodName; Moon.Orm.Util.LogUtil.Error(err); throw new Exception(err); } } else { if (methodRequestType == (int)RequestType.GET) { var err = "Current request is POST,but you didn't set a POST attribute to your method :" + classFullName + "->" + methodName; Moon.Orm.Util.LogUtil.Error(err); throw new Exception(err); } } object[] parameters = ParameterUtil.GetParametersObject(dontValidateRequest, context.Request, methodInfo, ref resultList, ref fluentValidateResultType); //---------验证参数---------- if (ValidateMode(resultList) == false) { baseControllerInstance.IsModelValidate = false; if (fluentValidateResultType == ErrorResultReturnType.Json) { var json = Moon.Orm.Util.JsonUtil.ConvertObjectToJson(resultList); context.Response.Write(json); context.Response.Flush(); //context.Response.End();//2015年9月13日14:03:43 return; } else if (fluentValidateResultType == ErrorResultReturnType.PureData) { baseControllerInstance.ValidationResults = resultList; } } else { baseControllerInstance.IsModelValidate = true; } // if (customAspectAttributes.Length > 0) { List <AspectAttribute> aspectAttributeList = new List <AspectAttribute>(); foreach (var oneAspect in customAspectAttributes) { aspectAttributeList.Add(oneAspect as AspectAttribute); } OrderByAspectAttributePriority(aspectAttributeList); foreach (var oneAspect in aspectAttributeList) { var aspectResult = oneAspect.BeforeInvoke(methodInfo, context); if (aspectResult == AspectResultType.Stop) { return; } } resultObj = MethodInvokeUtil.Invoke(classInstance, methodName, parameters.Length == 0 ? null : parameters); foreach (var oneAspect in aspectAttributeList) { var rr = oneAspect.AfterInvoke(methodInfo, context); if (rr == AspectResultType.Stop) { return; } } } else { resultObj = MethodInvokeUtil.Invoke(classInstance, methodName, parameters.Length == 0 ? null : parameters); } resultAttr.Response(context, classFullName, methodName, resultObj, viewData); }