由于公司需要用到接口式开发,所以所以业务逻辑都写在webapi ,web端主要做调用,为了使开发方便,特此封装出统一请求的方法,页面调用只需写接口地址就行,话不多说,直接上代码。
1 public class ProxyHandlerController : BaseController 2 { 3 // 4 // GET: /ProxyHandler/ 5 public ActionResult ProcessRequest() 6 { 7 try 8 { 9 var Request = HttpContext.Request; 10 string url = Request.QueryString["_URL"].ToString(); 11 var parameters = new Dictionary<string, string>(); 12 if (Request.HttpMethod.ToUpper() == "POST") 13 { 14 foreach (var item in Request.Form.Keys) 15 { 16 parameters.Add(item.ToString(), Request.Form[item.ToString()].ToString()); 17 } 18 } 19 else 20 { 21 foreach (var item in Request.QueryString.Keys) 22 { 23 if (item.ToString() != "_URL") 24 { 25 parameters.Add(item.ToString(), Request.QueryString[item.ToString()].ToString()); 26 27 } 28 } 29 } 31 var response = HttpRequestHelp.sendRequest(ProjectConst.ApiUrl + url, parameters, Request.HttpMethod, LogOnModel.token); 32 return Json(response, JsonRequestBehavior.AllowGet); 33 } 34 catch (Exception e) 35 { 36 return Json(e.Message, JsonRequestBehavior.AllowGet); 37 } 38 } }
原文地址:https://www.cnblogs.com/sanfor/p/8951784.html
时间: 2024-10-08 15:35:01