ashx具体代码:
public class GuanWangWC : IHttpHandler { public void ProcessRequest(HttpContext context) { try { string method = context.Request.Params["method"]; //post if (method == "post") { this.PostBack(context); return; } if (method == "get") { this.GetBack(context); return; } string strip = RequestUtils.GetIp(context); string msg = "访问IP:" + strip; ShowResult(context, "99", msg); } catch (Exception ex) { LogUtils.Error("ashx出错=" + ex.Message); ShowResult(context, "100", ex.Message); } } private void GetBack(HttpContext context) { string strReq = HttpUtility.UrlDecode(context.Request.QueryString.ToString()); LogUtils.Info("请求参数:" + strReq ); SqlConnection conn = new SqlConnection("连接字符串"); try { ShowResult(context, "0", "ok"); return; } catch (Exception ex) { ShowResult(context, "99", "请求:" + strReq + "==错误信息:" + ex.Message); LogUtils.Error("请求参数:" + strReq + " 错误信息:" + ex.Message); } finally { conn.Close(); } } private void PostBack(HttpContext context) { string strReq = new httppost().GetData(context.Request); LogUtils.Info(string.Format("请求参数:{0}====", strReq)); TicketingRequset shr = SerializeUtil.DeserializeJson<TicketingRequset>(strReq); if (shr == null) { ShowResult(context, "99", "数据不能为空"); return; } SqlConnection conn = new SqlConnection("连接字符串"); try { ShowResult(context, "0", "ok"); return; } catch (Exception ex) { ShowResult(context, "99", "请求:" + strReq + "==错误信息:" + ex.Message); LogUtils.Error("====请求参数:" + strReq + " 错误信息:" + ex.Message); } finally { conn.Close(); } } /// <summary> /// /// </summary> /// <param name="context"></param> /// <param name="errorCode">0:成功 其他失败</param> /// <param name="errorMsg"></param> private void ShowResult(HttpContext context, string errorCode, string errorMsg) { string result = string.Format("{{\"errorCode\":{0},\"errorMsg\":\"{1}\"}}", errorCode, errorMsg); context.Response.Write(result); } public bool IsReusable { get { return false; } } }
ashx通用返回model,具体代码:
public class OrderData : IHttpHandler { public void ProcessRequest(HttpContext context) { string type = context.Request.Params["type"]; if (string.IsNullOrEmpty(type)) { Msg(context, new RspModel<object>(99, "type null")); return; } try { if (type == "AdjWriteLine") { AdjWriteLineMethod(context); return; } } catch (Exception ex) { Msg(context, new RspModel<object>(98, ex.Message)); Common.logclass.Error("出错:" + ex.Message, ex); return; } } private void AdjWriteLineMethod(HttpContext context) { string strreq = httputil.GetData(context.Request); // arr = context.Request.Params["arr"], var req = SerializeUtil.DeserializeJson<AdjWriteLineReq>(strreq); if (req == null) { logclass.Info("AdjWriteLineMethod请求:" + strreq); Msg(context, new RspModel<AdjWriteLineReq>(98, "序列化数据为空(请以json字符post提交)")); return; } SqlConnection conn = new SqlConnection(ConfigSetting.SqlConnection); try { RspModel<AdjWriteLineReq> rsp = new RspModel<AdjWriteLineReq>(0, ""); rsp.content = _model;//对应泛型 T Msg(context, rsp); return; } catch (Exception ex) { logclass.Info("AdjWriteLineMethod请求:" + strreq); logclass.Error("错误:==" + ex.Message); Msg(context, new RspModel<AdjWriteLineReq>(97, "错误:==" + ex.Message)); return; } finally { if (conn != null) conn.Dispose(); } } private void Msg<T>(HttpContext context, T model) { string result = SerializeUtil.SerializeJson(model); context.Response.ContentType = "text/plain"; context.Response.Write(result); } #region Model public class RspModel<T> { /// <summary> /// 0:成功 其他失败 /// </summary> public int code { get; set; } public string msg { get; set; } public T content { get; set; } public RspModel() { code = 99; } public RspModel(int _code, string _msg) { code = _code; msg = _msg; } } #endregion public bool IsReusable { get { return false; } } }