一般處理程式獲取get,post資料
阿新 • • 發佈:2018-11-29
ashx具體程式碼:
public class GuanWangWC : IHttpHandler { public void ProcessRequest(HttpContext context) { try { string method = context.Request.Params["method"]; //post if (method == "post") {View Codethis.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; } } }View Code