private void InvokeClass() { Type t = null; //ViewController是由页面的前两个路径决定了。 string[] items = QueryTool.GetLocalPath().Trim('/').Split('/'); string className = InvokeLogic.Const.Default; if (RouteConfig.RouteMode == 1) { className = items[0]; } else if (RouteConfig.RouteMode == 2) { className = items.Length > 1 ? items[0] + "." + items[1] : ""; } t = InvokeLogic.GetController(className); if (t == null) { WriteError("You need a " + className + " controller for coding!"); } else { try { object o = Activator.CreateInstance(t);//实例化 t.GetMethod("ProcessRequest").Invoke(o, new object[] { context }); } catch (ThreadAbortException e) { //内部提前Response.End()时引发的异常 //ASP.NET 的机制就是通过异常退出线程(不要觉的奇怪) } catch (Exception err) { WriteError(err.Message); } } //context.Response.End(); }
private void InvokeClass() { string localPath = context.Request.Url.LocalPath; Type t = null; if (localPath.IndexOf(".") == -1) // 处理Mvc请求 { //ViewController是由页面的前两个路径决定了。 string[] items = localPath.Trim('/').Split('/'); string className = InvokeLogic.Default; if (RouteConfig.RouteMode == 1) { className = items[0]; } else if (RouteConfig.RouteMode == 2) { className = items.Length > 1 ? items[1] : ""; } t = InvokeLogic.GetType(className); if (t == null) { WriteError("You need a controller for coding!"); } try { object o = Activator.CreateInstance(t);//实例化 t.GetMethod("ProcessRequest").Invoke(o, new object[] { context }); } catch (ThreadAbortException e) { //ASP.NET 的机制就是通过异常退出线程(不要觉的奇怪) } catch (Exception err) { WriteError(err.Message); } } }
public void ProcessRequest(HttpContext context) { this.context = context; try { Type t = _ControllerType = this.GetType(); string[] items = QueryTool.GetLocalPath().Trim('/').Split('/'); string methodName = string.Empty; switch (RouteConfig.RouteMode) { case 0: methodName = items[0]; break; case 1: if (items.Length > 1) { methodName = items[1]; } break; case 2: if (items.Length > 2) { methodName = items[2]; } break; } bool isGoOn = true; //if (InvokeLogic.BeforeInvokeMethod != null) //{ // isGoOn = Convert.ToBoolean(InvokeLogic.BeforeInvokeMethod.Invoke(null, new object[] { this, methodName })); //} //if (isGoOn) //{ char[] attrFlags; MethodInfo method = InvokeLogic.GetMethod(t, methodName, out attrFlags); if (method != null) { if (attrFlags[0] == '1') { #region Invoke CheckToken MethodInfo checkToken = InvokeLogic.GetMethod(t, InvokeLogic.CheckToken); if (checkToken != null && checkToken.Name == InvokeLogic.CheckToken) { isGoOn = Convert.ToBoolean(checkToken.Invoke(this, null)); } else if (InvokeLogic.CheckTokenMethod != null) { isGoOn = Convert.ToBoolean(InvokeLogic.CheckTokenMethod.Invoke(null, new object[] { this, methodName })); } #endregion } if (isGoOn && attrFlags[1] != attrFlags[2])//配置了HttpGet或HttpPost { if (attrFlags[1] == '1' && !IsHttpGet) { isGoOn = false; Write("Only support HttpGet!", false); } else if (attrFlags[2] == '1' && !IsHttpPost) { isGoOn = false; Write("Only support HttpPost!", false); } } if (isGoOn) { #region Method Invoke _Action = method.Name; MethodInfo beforeInvoke = InvokeLogic.GetMethod(t, InvokeLogic.BeforeInvoke); if (beforeInvoke != null && beforeInvoke.Name == InvokeLogic.BeforeInvoke) { isGoOn = Convert.ToBoolean(beforeInvoke.Invoke(this, new object[] { method.Name })); } else if (InvokeLogic.BeforeInvokeMethod != null) { isGoOn = Convert.ToBoolean(InvokeLogic.BeforeInvokeMethod.Invoke(null, new object[] { this, methodName })); } //BeforeInvoke(method.Name); if (!CancelLoadHtml) { _View = ViewEngine.Create(t.Name, method.Name); } if (isGoOn) { method.Invoke(this, GetInvokeParas(method)); if (IsHttpPost) { string name = GetBtnName(); if (!string.IsNullOrEmpty(name)) { MethodInfo postBtnMethod = InvokeLogic.GetMethod(t, name); if (postBtnMethod != null && postBtnMethod.Name != InvokeLogic.Default) { postBtnMethod.Invoke(this, GetInvokeParas(postBtnMethod)); } } } if (isGoOn) { MethodInfo endInvoke = InvokeLogic.GetMethod(t, InvokeLogic.EndInvoke); if (endInvoke != null && endInvoke.Name == InvokeLogic.EndInvoke) { isGoOn = Convert.ToBoolean(endInvoke.Invoke(this, new object[] { method.Name })); } else if (InvokeLogic.EndInvokeMethod != null) { InvokeLogic.EndInvokeMethod.Invoke(null, new object[] { this, methodName }); } // EndInvoke(method.Name); } } #endregion } } //} if (string.IsNullOrEmpty(context.Response.Charset)) { context.Response.Charset = "utf-8"; } if (View != null) { context.Response.Write(View.OutXml); } else if (apiResult.Length > 0) { if (apiResult[0] == '{' && apiResult[apiResult.Length - 1] == '}') { context.Response.ContentType = "application/json"; } context.Response.Write(apiResult.ToString()); } } catch (System.Threading.ThreadAbortException) { return; } catch (Exception err) { string errMssg = err.InnerException != null ? err.InnerException.Message : err.Message; WriteLog(errMssg); context.Response.Write(errMssg); } }
public void ProcessRequest(HttpContext context) { this.context = context; try { Type t = _ControllerType = this.GetType(); string[] items = QueryTool.GetLocalPath().Trim('/').Split('/'); string methodName = string.Empty; switch (RouteConfig.RouteMode) { case 0: methodName = items[0]; break; case 1: if (items.Length > 1) { methodName = items[1]; } break; case 2: if (items.Length > 2) { methodName = items[2]; } break; } MethodInfo method = InvokeLogic.GetMethod(t, methodName); if (method != null) { _Action = method.Name; BeforeInvoke(method.Name); if (!CancelLoadHtml) { _View = ViewEngine.Create(t.Name, method.Name); } //#if DEBUG // string text = "Invoke " + t.FullName + "." + Action + "(" + Para + ")<hr />"; // if (_View != null) // { // _View.AppendNode(_View.GetList("body")[0], _View.CreateNode("div", text), 0); // } // else // { // System.Web.HttpContext.Current.Response.Write(text); // } //#endif if (!CancelInvoke) { method.Invoke(this, null); if (IsHttpPost) { string name = GetBtnName(); if (!string.IsNullOrEmpty(name)) { MethodInfo postBtnMethod = InvokeLogic.GetMethod(t, name); if (postBtnMethod != null && postBtnMethod.Name != InvokeLogic.Default) { postBtnMethod.Invoke(this, null); } } } if (!CancelInvoke) { EndInvoke(method.Name); } } if (View != null) { context.Response.Write(View.OutXml); } else if (apiResult.Length > 0) { context.Response.Write(apiResult.ToString()); } } } catch (System.Threading.ThreadAbortException) { return; } catch (Exception err) { WriteError(err.Message); context.Response.Write(err.Message); } context.Response.End(); }
public void ProcessRequest(HttpContext context) { this.context = context; try { Type t = _ControllerType = this.GetType(); #region GetMethodName string[] items = QueryTool.GetLocalPath().Trim('/').Split('/'); string methodName = string.Empty; switch (RouteConfig.RouteMode) { case 0: methodName = items[0]; break; case 1: if (items.Length > 1) { methodName = items[1]; } break; case 2: _Module = items[0]; if (items.Length > 2) { methodName = items[2]; } break; } _Action = methodName; #endregion bool isGoOn = true; AttributeList attrFlags; MethodInfo method = InvokeLogic.GetMethod(t, methodName, out attrFlags); if (method != null) { if (isGoOn)//配置了HttpGet或HttpPost { isGoOn = attrFlags.HasKey(Request.HttpMethod); if (!isGoOn) { Write("Http method not support " + Request.HttpMethod, false); } } if (isGoOn && attrFlags.HasAck)//有[Ack] { #region Validate CheckAck MethodInfo checkAck = InvokeLogic.GetMethod(t, InvokeLogic.Const.CheckAck); if (checkAck != null && checkAck.Name == InvokeLogic.Const.CheckAck) { isGoOn = Convert.ToBoolean(checkAck.Invoke(this, null)); } else if (InvokeLogic.DefaultCheckAck != null) { isGoOn = Convert.ToBoolean(InvokeLogic.DefaultCheckAck.Invoke(null, new object[] { this, methodName })); } #endregion } if (isGoOn && attrFlags.HasToken)//有[Token] { #region Validate CheckToken MethodInfo checkToken = InvokeLogic.GetMethod(t, InvokeLogic.Const.CheckToken); if (checkToken != null && checkToken.Name == InvokeLogic.Const.CheckToken) { isGoOn = Convert.ToBoolean(checkToken.Invoke(this, null)); } else if (InvokeLogic.DefaultCheckToken != null) { isGoOn = Convert.ToBoolean(InvokeLogic.DefaultCheckToken.Invoke(null, new object[] { this, methodName })); } else if (InvokeLogic.AuthCheckToken != null) { isGoOn = Convert.ToBoolean(InvokeLogic.AuthCheckToken.Invoke(null, new object[] { this })); } #endregion } if (isGoOn) { #region Method Invoke #region BeforeInvoke if (InvokeLogic.BeforeInvokeMethod != null)//先调用全局 { isGoOn = Convert.ToBoolean(InvokeLogic.BeforeInvokeMethod.Invoke(null, new object[] { this, methodName })); } if (isGoOn) { MethodInfo beforeInvoke = InvokeLogic.GetMethod(t, InvokeLogic.Const.BeforeInvoke); if (beforeInvoke != null && beforeInvoke.Name == InvokeLogic.Const.BeforeInvoke) { isGoOn = Convert.ToBoolean(beforeInvoke.Invoke(this, new object[] { method.Name })); } } #endregion //BeforeInvoke(method.Name); if (!CancelLoadHtml) { _View = ViewEngine.Create(t.Name, method.Name); if (_View != null) { //追加几个全局标签变量 _View.KeyValue.Add("module", Module.ToLower()); _View.KeyValue.Add("controller", ControllerType.Name.ToLower()); _View.KeyValue.Add("action", Action.ToLower()); _View.KeyValue.Add("para", Para.ToLower()); } } if (isGoOn) { object[] paras; if (GetInvokeParas(method, out paras)) { method.Invoke(this, paras); if (IsHttpPost) { #region Button Invoke string name = GetBtnName(); if (!string.IsNullOrEmpty(name)) { MethodInfo postBtnMethod = InvokeLogic.GetMethod(t, name); if (postBtnMethod != null && postBtnMethod.Name != InvokeLogic.Const.Default) { GetInvokeParas(postBtnMethod, out paras); postBtnMethod.Invoke(this, paras); } } #endregion } if (isGoOn) { #region EndInvoke MethodInfo endInvoke = InvokeLogic.GetMethod(t, InvokeLogic.Const.EndInvoke); if (endInvoke != null && endInvoke.Name == InvokeLogic.Const.EndInvoke) { endInvoke.Invoke(this, new object[] { method.Name }); } if (InvokeLogic.EndInvokeMethod != null) { InvokeLogic.EndInvokeMethod.Invoke(null, new object[] { this, methodName }); } #endregion //if (InvokeLogic.DocRecord != null) //{ // InvokeLogic.DocRecord.Invoke(null, new object[] { this, methodName }); //} } } } #endregion } } if (string.IsNullOrEmpty(context.Response.Charset)) { context.Response.Charset = "utf-8"; } if (View != null) { context.Response.Write(View.OutXml); } else if (apiResult.Length > 0) { string outResult = apiResult.ToString(); if (string.IsNullOrEmpty(context.Response.ContentType)) { context.Response.ContentType = "text/html"; } if (context.Response.ContentType == "text/html") { if (apiResult[0] == '{' && apiResult[apiResult.Length - 1] == '}') { context.Response.ContentType = "application/json"; } else if (outResult.StartsWith("<?xml") && apiResult[apiResult.Length - 1] == '>') { context.Response.ContentType = "application/xml"; } } context.Response.Write(outResult); } } catch (System.Threading.ThreadAbortException) { return; } catch (Exception err) { string errMssg = err.InnerException != null ? err.InnerException.Message : err.Message; WriteLog(errMssg); if (View == null) { errMssg = JsonHelper.OutResult(false, errMssg); } context.Response.Write(errMssg); } }
public void ProcessRequest(HttpContext context) { this.context = context; try { Type t = _ControllerType = this.GetType(); #region GetMethodName string[] items = QueryTool.GetLocalPath().Trim('/').Split('/'); string methodName = string.Empty; switch (RouteConfig.RouteMode) { case 0: methodName = items[0]; break; case 1: if (items.Length > 1) { methodName = items[1]; } break; case 2: _Module = items[0]; if (items.Length > 2) { methodName = items[2]; } break; } _Action = methodName; #endregion bool isGoOn = true; char[] attrFlags; MethodInfo method = InvokeLogic.GetMethod(t, methodName, out attrFlags); if (method != null) { if (attrFlags[0] == '1') { #region Validate CheckToken MethodInfo checkToken = InvokeLogic.GetMethod(t, InvokeLogic.Const.CheckToken); if (checkToken != null && checkToken.Name == InvokeLogic.Const.CheckToken) { isGoOn = Convert.ToBoolean(checkToken.Invoke(this, null)); } else if (InvokeLogic.DefaultCheckToken != null) { isGoOn = Convert.ToBoolean(InvokeLogic.DefaultCheckToken.Invoke(null, new object[] { this, methodName })); } else if (InvokeLogic.AuthCheckToken != null) { isGoOn = Convert.ToBoolean(InvokeLogic.AuthCheckToken.Invoke(null, new object[] { this })); } #endregion } if (isGoOn)//配置了HttpGet或HttpPost { #region Validate Http methods string[] httpMethods = InvokeLogic.HttpMethods; for (int i = 0; i < httpMethods.Length; i++) { if (attrFlags[i + 1] == '1') { isGoOn = false; if (httpMethods[i] == Request.HttpMethod) { isGoOn = true; break; } } } if (!isGoOn) { Write("Http method not support " + Request.HttpMethod, false); } #endregion } if (isGoOn) { #region Method Invoke #region BeforeInvoke MethodInfo beforeInvoke = InvokeLogic.GetMethod(t, InvokeLogic.Const.BeforeInvoke); if (beforeInvoke != null && beforeInvoke.Name == InvokeLogic.Const.BeforeInvoke) { isGoOn = Convert.ToBoolean(beforeInvoke.Invoke(this, new object[] { method.Name })); } else if (InvokeLogic.BeforeInvokeMethod != null) { isGoOn = Convert.ToBoolean(InvokeLogic.BeforeInvokeMethod.Invoke(null, new object[] { this, methodName })); } #endregion //BeforeInvoke(method.Name); if (!CancelLoadHtml) { _View = ViewEngine.Create(t.Name, method.Name); } if (isGoOn) { object[] paras; if (GetInvokeParas(method, out paras)) { method.Invoke(this, paras); if (IsHttpPost) { #region Button Invoke string name = GetBtnName(); if (!string.IsNullOrEmpty(name)) { MethodInfo postBtnMethod = InvokeLogic.GetMethod(t, name); if (postBtnMethod != null && postBtnMethod.Name != InvokeLogic.Const.Default) { GetInvokeParas(postBtnMethod, out paras); postBtnMethod.Invoke(this, paras); } } #endregion } if (isGoOn) { #region EndInvoke MethodInfo endInvoke = InvokeLogic.GetMethod(t, InvokeLogic.Const.EndInvoke); if (endInvoke != null && endInvoke.Name == InvokeLogic.Const.EndInvoke) { isGoOn = Convert.ToBoolean(endInvoke.Invoke(this, new object[] { method.Name })); } else if (InvokeLogic.EndInvokeMethod != null) { InvokeLogic.EndInvokeMethod.Invoke(null, new object[] { this, methodName }); } #endregion if (InvokeLogic.DocRecord != null) { InvokeLogic.DocRecord.Invoke(null, new object[] { this, methodName }); } } } } #endregion } } if (string.IsNullOrEmpty(context.Response.Charset)) { context.Response.Charset = "utf-8"; } if (View != null) { context.Response.Write(View.OutXml); } else if (apiResult.Length > 0) { if (apiResult[0] == '{' && apiResult[apiResult.Length - 1] == '}') { context.Response.ContentType = "application/json"; } context.Response.Write(apiResult.ToString()); } } catch (System.Threading.ThreadAbortException) { return; } catch (Exception err) { string errMssg = err.InnerException != null ? err.InnerException.Message : err.Message; WriteLog(errMssg); if (View == null) { errMssg = JsonHelper.OutResult(false, errMssg); } context.Response.Write(errMssg); } }
public void ProcessRequest(HttpContext context) { this.context = context; try { Type t = _ControllerType = this.GetType(); string[] items = QueryTool.GetLocalPath().Trim('/').Split('/'); string methodName = string.Empty; switch (RouteConfig.RouteMode) { case 0: methodName = items[0]; break; case 1: if (items.Length > 1) { methodName = items[1]; } break; case 2: if (items.Length > 2) { methodName = items[2]; } break; } bool hasTokenAttr = false; MethodInfo method = InvokeLogic.GetMethod(t, methodName, out hasTokenAttr); if (method != null) { bool isGoOn = true; if (hasTokenAttr && InvokeLogic.CheckTokenMethod != null) { //CheckToken isGoOn = Convert.ToBoolean(InvokeLogic.CheckTokenMethod.Invoke(null, new object[] { this, methodName })); } if (isGoOn) { _Action = method.Name; BeforeInvoke(method.Name); if (!CancelLoadHtml) { _View = ViewEngine.Create(t.Name, method.Name); } if (!CancelInvoke) { method.Invoke(this, null); if (IsHttpPost) { string name = GetBtnName(); if (!string.IsNullOrEmpty(name)) { MethodInfo postBtnMethod = InvokeLogic.GetMethod(t, name); if (postBtnMethod != null && postBtnMethod.Name != InvokeLogic.Default) { postBtnMethod.Invoke(this, null); } } } if (!CancelInvoke) { EndInvoke(method.Name); } } } if (View != null) { context.Response.Write(View.OutXml); } else if (apiResult.Length > 0) { context.Response.Write(apiResult.ToString()); } } } catch (System.Threading.ThreadAbortException) { return; } catch (Exception err) { WriteError(err.Message); context.Response.Write(err.Message); } context.Response.End(); }
public void ProcessRequest(HttpContext context) { this.context = context; try { Type t = _ControllerType = this.GetType(); string[] items = QueryTool.GetLocalPath().Trim('/').Split('/'); string methodName = string.Empty; switch (RouteConfig.RouteMode) { case 0: methodName = items[0]; break; case 1: if (items.Length > 1) { methodName = items[1]; } break; case 2: if (items.Length > 2) { methodName = items[2]; } break; } bool isGoOn = true; if (InvokeLogic.BeforeInvokeMethod != null) { isGoOn = Convert.ToBoolean(InvokeLogic.BeforeInvokeMethod.Invoke(null, new object[] { this, methodName })); } if (isGoOn) { char[] attrFlags; MethodInfo method = InvokeLogic.GetMethod(t, methodName, out attrFlags); if (method != null) { if (attrFlags[0] == '1') { #region CheckToken MethodInfo checkToken = InvokeLogic.GetMethod(t, InvokeLogic.CheckToken); if (checkToken != null && checkToken.Name == InvokeLogic.CheckToken) { isGoOn = Convert.ToBoolean(checkToken.Invoke(this, null)); } else if (InvokeLogic.CheckTokenMethod != null) { isGoOn = Convert.ToBoolean(InvokeLogic.CheckTokenMethod.Invoke(null, new object[] { this, methodName })); } #endregion } if (isGoOn && attrFlags[1] != attrFlags[2])//配置了HttpGet或HttpPost { if (attrFlags[1] == '1' && !IsHttpGet) { isGoOn = false; Write("Only support HttpGet!", false); } else if (attrFlags[2] == '1' && !IsHttpPost) { isGoOn = false; Write("Only support HttpPost!", false); } } if (isGoOn) { #region Method Invoke _Action = method.Name; BeforeInvoke(method.Name); if (!CancelLoadHtml) { _View = ViewEngine.Create(t.Name, method.Name); } if (!CancelInvoke) { method.Invoke(this, null); if (IsHttpPost) { string name = GetBtnName(); if (!string.IsNullOrEmpty(name)) { MethodInfo postBtnMethod = InvokeLogic.GetMethod(t, name); if (postBtnMethod != null && postBtnMethod.Name != InvokeLogic.Default) { postBtnMethod.Invoke(this, null); } } } if (!CancelInvoke) { EndInvoke(method.Name); } } #endregion } } } if (View != null) { context.Response.Write(View.OutXml); } else if (apiResult.Length > 0) { context.Response.Write(apiResult.ToString()); } } catch (System.Threading.ThreadAbortException) { return; } catch (Exception err) { string errorMsg = string.Empty; if (err.InnerException != null) { errorMsg = string.Format("异常信息:{0}" + Environment.NewLine + "堆栈信息:{1}。" , err.InnerException.Message , err.InnerException.StackTrace); } else { errorMsg = string.Format("异常信息:{0}" + Environment.NewLine + "堆栈信息:{1}。" , err.Message, err.StackTrace); } WriteError(errorMsg); if (AppSettings.IsDevMode)//开发者模式 { context.Response.Write(errorMsg); } else { context.Response.Write(err.Message); } } if (string.IsNullOrEmpty(context.Response.Charset)) { context.Response.Charset = "utf-8"; } }