由于之前遇到的easyui调用webapi的问题。
参见 :
所以就考虑,封装一个泛型用来返回 HttpResponseMessage
直接上代码:
泛型封装:
public class APIResultwebapi:: HttpResponseMessage { private T Data; public APIResult(T Data, string DataType = "application/json") { this.Data = Data; string jsonStr = GetJson(this.Data); this.Content = new StringContent(jsonStr, System.Text.Encoding.UTF8, DataType); }//返回值 public bool ResponseResult = false;//返回message public string ResponseMsg = "failed"; private string GetJson(T Data) { if (Data is string || Data is StringBuilder) return Data.ToString(); else if (Data is DataTable) return JsonConvert.SerializeObject(Data, new DataTableConverter()); else return JsonConvert.SerializeObject(Data); } }
这里是返回DataTable类型
public APIResultGetProductParentType(string PT_Name=null, string PT_Code=null, int PT_ParentID = 0) { DataTable dt = pt.GetProductParentType(PT_Name, PT_Code,PT_ParentID); APIResult result = new APIResult (dt); return result; }
这里是返回对象类型
public APIResult前台HTML :Get(int id) { Rootobject resp=new Rootobject(); APIResult api = new APIResult (resp); return api; }