創(chuàng)新互聯(lián)專注于始興網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠為您提供始興營銷型網(wǎng)站建設(shè),始興網(wǎng)站制作、始興網(wǎng)頁設(shè)計(jì)、始興網(wǎng)站官網(wǎng)定制、小程序定制開發(fā)服務(wù),打造始興網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供始興網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.Linq; using System.Reflection; using System.Web; using System.ComponentModel; using System.Web.Script.Serialization; namespace HHSoft.PSMIS.Web.WebSite.UserHandler { ///d/// HandlerBase 的摘要說明 /// public class HandlerBase : IHttpHandler { ////// 指定過來的http請求類型 主要指定action方法名稱的接收方式 get 或者 post /// protected NameValueCollection httpReuqest = HttpContext.Current.Request.Form; ////// 指定返回頭 /// protected string contentType = "text/plain"; ////// 返回值類型 /// public ReturnType returnType = ReturnType.json; ////// 指定接收action方法的參數(shù)名稱 /// protected string actionName = "action"; //獲取當(dāng)前的http context protected HttpContext Context { get { return HttpContext.Current; } } public void Proce***equest(HttpContext context) { //RequestType = context.Request.ServerVariables["Request_Method"]; string requestContentType =""; string requestReturnType=""; requestContentType = httpReuqest["contentType"]; requestReturnType = httpReuqest["returnType"]; if (!string.IsNullOrEmpty(requestReturnType)) { returnType = (ReturnType)Enum.Parse(typeof(ReturnType), requestReturnType); } if (string.IsNullOrEmpty(requestContentType)) { context.Response.ContentType = this.contentType; } else { context.Response.ContentType = requestContentType; } try { //動態(tài)調(diào)用方法 當(dāng)然 你還可以在這里加上是否為同域名請求的判斷 this.DynamicMethod(); } catch (AmbiguousMatchException amEx) { if (returnType == ReturnType.json) { this.PrintErrorJson(string.Format("根據(jù)該參數(shù){0}找到了多個方法", amEx.Message)); } else { this.Context.Response.Write(string.Format("error,根據(jù)該參數(shù){0}找到了多個方法", amEx.Message)); } } catch (ArgumentException argEx) { if (returnType == ReturnType.json) { this.PrintErrorJson("參數(shù)異常" + argEx.Message); } else { this.Context.Response.Write("error,參數(shù)異常" + argEx.Message); } } catch (ApplicationException apEx) { if (returnType == ReturnType.json) { this.PrintErrorJson("程序異常" + apEx.Message); } else { this.Context.Response.Write("error,程序異常" + apEx.Message); } } } #region 動態(tài)調(diào)用方法 ////// 動態(tài)調(diào)用方法 /// private void DynamicMethod() { //根據(jù)指定的請求類型獲取方法名 string action = this.httpReuqest[this.actionName]; if (!string.IsNullOrEmpty(action)) { //獲取方法的實(shí)例 非靜態(tài) 需要Public訪問權(quán)限 忽略大小寫 MethodInfo methodInfo = this.GetType().GetMethod(action, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (methodInfo != null) { //調(diào)用方法 methodInfo.Invoke(this, null); } else { throw new ApplicationException(string.Format("沒有找到方法{0}", action)); } } else { throw new ArgumentNullException("沒有找到調(diào)用方法參數(shù)或者方法名為空"); } } #endregion #region 打印Json的相關(guān)處理 #region 打印Json的相關(guān)處理 ////// 打印遇到異常的json /// /// protected void PrintErrorJson(string msg) { this.PrintJson("error", msg); } ////// 打印成功處理的json /// /// protected void PrintSuccessJson(string msg) { this.PrintJson("success", msg); } ////// 打印json /// /// /// protected void PrintJson(string state, string msg) { this.Context.Response.Write("{\"state\":\"" + state + "\",\"msg\":\"" + msg + "\"}"); } protected void PrintString(string obj) { this.Context.Response.Write(obj); } #endregion #endregion public bool IsReusable { get { return false; } } } public enum ReturnType { ////// 返回字符串 /// [Description("返回字符串")] text=0, ////// 返回json類型 /// [Description("返回json類型")] json=1 } }執(zhí)勤啊
////// HandlerBase 的摘要說明 /// public class HandlerBase : IHttpHandler { ////// 指定過來的http請求類型 主要指定action方法名稱的接收方式 get 或者 post /// protected NameValueCollection httpReuqest = HttpContext.Current.Request.Form; ////// 指定返回頭 /// protected string contentType = "text/plain"; ////// 返回值類型 /// public ReturnType returnType = ReturnType.json; ////// 指定接收action方法的參數(shù)名稱 /// protected string actionName = "action"; //獲取當(dāng)前的http context protected HttpContext Context { get { return HttpContext.Current; } } public void Proce***equest(HttpContext context) { //RequestType = context.Request.ServerVariables["Request_Method"]; string requestContentType =""; string requestReturnType=""; requestContentType = httpReuqest["contentType"]; requestReturnType = httpReuqest["returnType"]; if (!string.IsNullOrEmpty(requestReturnType)) { returnType = (ReturnType)Enum.Parse(typeof(ReturnType), requestReturnType); } if (string.IsNullOrEmpty(requestContentType)) { context.Response.ContentType = this.contentType; } else { context.Response.ContentType = requestContentType; } try { //動態(tài)調(diào)用方法 當(dāng)然 你還可以在這里加上是否為同域名請求的判斷 this.DynamicMethod(); } catch (AmbiguousMatchException amEx) { if (returnType == ReturnType.json) { this.PrintErrorJson(string.Format("根據(jù)該參數(shù){0}找到了多個方法", amEx.Message)); } else { this.Context.Response.Write(string.Format("error,根據(jù)該參數(shù){0}找到了多個方法", amEx.Message)); } } catch (ArgumentException argEx) { if (returnType == ReturnType.json) { this.PrintErrorJson("參數(shù)異常" + argEx.Message); } else { this.Context.Response.Write("error,參數(shù)異常" + argEx.Message); } } catch (ApplicationException apEx) { if (returnType == ReturnType.json) { this.PrintErrorJson("程序異常" + apEx.Message); } else { this.Context.Response.Write("error,程序異常" + apEx.Message); } } } #region 動態(tài)調(diào)用方法 ////// 動態(tài)調(diào)用方法 /// private void DynamicMethod() { //根據(jù)指定的請求類型獲取方法名 string action = this.httpReuqest[this.actionName]; if (!string.IsNullOrEmpty(action)) { //獲取方法的實(shí)例 非靜態(tài) 需要Public訪問權(quán)限 忽略大小寫 MethodInfo methodInfo = this.GetType().GetMethod(action, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (methodInfo != null) { //調(diào)用方法 methodInfo.Invoke(this, null); } else { throw new ApplicationException(string.Format("沒有找到方法{0}", action)); } } else { throw new ArgumentNullException("沒有找到調(diào)用方法參數(shù)或者方法名為空"); } } #endregion #region 打印Json的相關(guān)處理 #region 打印Json的相關(guān)處理 ////// 打印遇到異常的json /// /// protected void PrintErrorJson(string msg) { this.PrintJson("error", msg); } ////// 打印成功處理的json /// /// protected void PrintSuccessJson(string msg) { this.PrintJson("success", msg); } ////// 打印json /// /// /// protected void PrintJson(string state, string msg) { this.Context.Response.Write("{\"state\":\"" + state + "\",\"msg\":\"" + msg + "\"}"); } protected void PrintString(string obj) { this.Context.Response.Write(obj); } #endregion #endregion public bool IsReusable { get { return false; } } } public enum ReturnType { ////// 返回字符串 /// [Description("返回字符串")] text=0, ////// 返回json類型 /// [Description("返回json類型")] json=1 } 之前