c#web项目请求样例
创新互联专注于始兴网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供始兴营销型网站建设,始兴网站制作、始兴网页设计、始兴网站官网定制、小程序定制开发服务,打造始兴网络公司原创品牌,更为您提供始兴网站排名全网营销落地服务。
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方法的参数名称 /// protected string actionName = "action"; //获取当前的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 { //动态调用方法 当然 你还可以在这里加上是否为同域名请求的判断 this.DynamicMethod(); } catch (AmbiguousMatchException amEx) { if (returnType == ReturnType.json) { this.PrintErrorJson(string.Format("根据该参数{0}找到了多个方法", amEx.Message)); } else { this.Context.Response.Write(string.Format("error,根据该参数{0}找到了多个方法", amEx.Message)); } } catch (ArgumentException argEx) { if (returnType == ReturnType.json) { this.PrintErrorJson("参数异常" + argEx.Message); } else { this.Context.Response.Write("error,参数异常" + argEx.Message); } } catch (ApplicationException apEx) { if (returnType == ReturnType.json) { this.PrintErrorJson("程序异常" + apEx.Message); } else { this.Context.Response.Write("error,程序异常" + apEx.Message); } } } #region 动态调用方法 ////// 动态调用方法 /// private void DynamicMethod() { //根据指定的请求类型获取方法名 string action = this.httpReuqest[this.actionName]; if (!string.IsNullOrEmpty(action)) { //获取方法的实例 非静态 需要Public访问权限 忽略大小写 MethodInfo methodInfo = this.GetType().GetMethod(action, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (methodInfo != null) { //调用方法 methodInfo.Invoke(this, null); } else { throw new ApplicationException(string.Format("没有找到方法{0}", action)); } } else { throw new ArgumentNullException("没有找到调用方法参数或者方法名为空"); } } #endregion #region 打印Json的相关处理 #region 打印Json的相关处理 ////// 打印遇到异常的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 } }执勤啊
////// 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方法的参数名称 /// protected string actionName = "action"; //获取当前的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 { //动态调用方法 当然 你还可以在这里加上是否为同域名请求的判断 this.DynamicMethod(); } catch (AmbiguousMatchException amEx) { if (returnType == ReturnType.json) { this.PrintErrorJson(string.Format("根据该参数{0}找到了多个方法", amEx.Message)); } else { this.Context.Response.Write(string.Format("error,根据该参数{0}找到了多个方法", amEx.Message)); } } catch (ArgumentException argEx) { if (returnType == ReturnType.json) { this.PrintErrorJson("参数异常" + argEx.Message); } else { this.Context.Response.Write("error,参数异常" + argEx.Message); } } catch (ApplicationException apEx) { if (returnType == ReturnType.json) { this.PrintErrorJson("程序异常" + apEx.Message); } else { this.Context.Response.Write("error,程序异常" + apEx.Message); } } } #region 动态调用方法 ////// 动态调用方法 /// private void DynamicMethod() { //根据指定的请求类型获取方法名 string action = this.httpReuqest[this.actionName]; if (!string.IsNullOrEmpty(action)) { //获取方法的实例 非静态 需要Public访问权限 忽略大小写 MethodInfo methodInfo = this.GetType().GetMethod(action, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); if (methodInfo != null) { //调用方法 methodInfo.Invoke(this, null); } else { throw new ApplicationException(string.Format("没有找到方法{0}", action)); } } else { throw new ArgumentNullException("没有找到调用方法参数或者方法名为空"); } } #endregion #region 打印Json的相关处理 #region 打印Json的相关处理 ////// 打印遇到异常的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 } 之前
本文题目:c#web项目请求样例
URL分享:http://ybzwz.com/article/pijpgd.html