您好,登錄后才能下訂單哦!
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 { /// <summary> /// HandlerBase 的摘要說(shuō)明 /// </summary> public class HandlerBase : IHttpHandler { /// <summary> /// 指定過(guò)來(lái)的http請(qǐng)求類型 主要指定action方法名稱的接收方式 get 或者 post /// </summary> protected NameValueCollection httpReuqest = HttpContext.Current.Request.Form; /// <summary> /// 指定返回頭 /// </summary> protected string contentType = "text/plain"; /// <summary> /// 返回值類型 /// </summary> public ReturnType returnType = ReturnType.json; /// <summary> /// 指定接收action方法的參數(shù)名稱 /// </summary> 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 { //動(dòng)態(tài)調(diào)用方法 當(dāng)然 你還可以在這里加上是否為同域名請(qǐng)求的判斷 this.DynamicMethod(); } catch (AmbiguousMatchException amEx) { if (returnType == ReturnType.json) { this.PrintErrorJson(string.Format("根據(jù)該參數(shù){0}找到了多個(gè)方法", amEx.Message)); } else { this.Context.Response.Write(string.Format("error,根據(jù)該參數(shù){0}找到了多個(gè)方法", 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 動(dòng)態(tài)調(diào)用方法 /// <summary> /// 動(dòng)態(tài)調(diào)用方法 /// </summary> private void DynamicMethod() { //根據(jù)指定的請(qǐng)求類型獲取方法名 string action = this.httpReuqest[this.actionName]; if (!string.IsNullOrEmpty(action)) { //獲取方法的實(shí)例 非靜態(tài) 需要Public訪問(wèn)權(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("沒(méi)有找到方法{0}", action)); } } else { throw new ArgumentNullException("沒(méi)有找到調(diào)用方法參數(shù)或者方法名為空"); } } #endregion #region 打印Json的相關(guān)處理 #region 打印Json的相關(guān)處理 /// <summary> /// 打印遇到異常的json /// </summary> /// <param name="msg"></param> protected void PrintErrorJson(string msg) { this.PrintJson("error", msg); } /// <summary> /// 打印成功處理的json /// </summary> /// <param name="msg"></param> protected void PrintSuccessJson(string msg) { this.PrintJson("success", msg); } /// <summary> /// 打印json /// </summary> /// <param name="state"></param> /// <param name="msg"></param> 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 { /// <summary> /// 返回字符串 /// </summary> [Description("返回字符串")] text=0, /// <summary> /// 返回json類型 /// </summary> [Description("返回json類型")] json=1 } }執(zhí)勤啊d
/// <summary> /// HandlerBase 的摘要說(shuō)明 /// </summary> public class HandlerBase : IHttpHandler { /// <summary> /// 指定過(guò)來(lái)的http請(qǐng)求類型 主要指定action方法名稱的接收方式 get 或者 post /// </summary> protected NameValueCollection httpReuqest = HttpContext.Current.Request.Form; /// <summary> /// 指定返回頭 /// </summary> protected string contentType = "text/plain"; /// <summary> /// 返回值類型 /// </summary> public ReturnType returnType = ReturnType.json; /// <summary> /// 指定接收action方法的參數(shù)名稱 /// </summary> 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 { //動(dòng)態(tài)調(diào)用方法 當(dāng)然 你還可以在這里加上是否為同域名請(qǐng)求的判斷 this.DynamicMethod(); } catch (AmbiguousMatchException amEx) { if (returnType == ReturnType.json) { this.PrintErrorJson(string.Format("根據(jù)該參數(shù){0}找到了多個(gè)方法", amEx.Message)); } else { this.Context.Response.Write(string.Format("error,根據(jù)該參數(shù){0}找到了多個(gè)方法", 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 動(dòng)態(tài)調(diào)用方法 /// <summary> /// 動(dòng)態(tài)調(diào)用方法 /// </summary> private void DynamicMethod() { //根據(jù)指定的請(qǐng)求類型獲取方法名 string action = this.httpReuqest[this.actionName]; if (!string.IsNullOrEmpty(action)) { //獲取方法的實(shí)例 非靜態(tài) 需要Public訪問(wèn)權(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("沒(méi)有找到方法{0}", action)); } } else { throw new ArgumentNullException("沒(méi)有找到調(diào)用方法參數(shù)或者方法名為空"); } } #endregion #region 打印Json的相關(guān)處理 #region 打印Json的相關(guān)處理 /// <summary> /// 打印遇到異常的json /// </summary> /// <param name="msg"></param> protected void PrintErrorJson(string msg) { this.PrintJson("error", msg); } /// <summary> /// 打印成功處理的json /// </summary> /// <param name="msg"></param> protected void PrintSuccessJson(string msg) { this.PrintJson("success", msg); } /// <summary> /// 打印json /// </summary> /// <param name="state"></param> /// <param name="msg"></param> 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 { /// <summary> /// 返回字符串 /// </summary> [Description("返回字符串")] text=0, /// <summary> /// 返回json類型 /// </summary> [Description("返回json類型")] json=1 } 之前
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。