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
{
///
/// 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
}
}执勤啊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 } 之前
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

新闻标题:c#web项目请求样例-创新互联
网站URL:http://www.jxjierui.cn/article/digsss.html


咨询
建站咨询
