利用ASP.NET MVC+EasyUI+SqlServer搭建企业开发框架(5)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using System.Web; using System.Data; namespace formula { public class NewtonJsonResult : JsonResult { public override void ExecuteResult(ControllerContext context) { //确认是否用于响应HTTP-Get请求 if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Compare(context.HttpContext.Request.HttpMethod, "GET", true) == 0) { throw new InvalidOperationException("禁止Get请求"); } HttpResponseBase response = context.HttpContext.Response; //设置媒体类型和编码方式 response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType; if (this.ContentEncoding != null) { response.ContentEncoding = this.ContentEncoding; } //序列化对象,并写入当前的HttpResponse if (null == this.Data) return; if (this.Data is string) { response.Write(Data); } else if (this.Data is DataRow) { Dictionary<string, object> dic = new Dictionary<string, object>(); DataRow row = this.Data as DataRow; foreach (DataColumn col in row.Table.Columns) { dic.Add(col.ColumnName, row[col]); } response.Write(JsonHelper.ToJson(dic)); } else { response.Write(JsonHelper.ToJson(this.Data)); } } } }

6、Json序列化和反序列的帮助类JsonHelper:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using Newtonsoft.Json.Converters; using Newtonsoft.Json; namespace formula { public static class JsonHelper { public static string ToJson<T>(T obj) { if (obj == null || obj.ToString() == "null") return null; if (obj != null && (obj.GetType() == typeof(String) || obj.GetType() == typeof(string))) { return obj.ToString(); } IsoDateTimeConverter dt = new IsoDateTimeConverter(); dt.DateTimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss"; return JsonConvert.SerializeObject(obj, dt); } /// <summary> /// 从一个Json串生成对象信息 /// </summary> /// <param>JSON字符串</param> /// <typeparam>对象类型</typeparam> /// <returns></returns> public static T ToObject<T>(string json) where T : class { if (String.IsNullOrEmpty(json)) return null; T obj = JsonConvert.DeserializeObject<T>(json); return obj; } /// <summary> /// 返回 Diction<string,object> /// </summary> /// <param></param> /// <returns></returns> public static Dictionary<string, object> ToObject(string json) { if (String.IsNullOrEmpty(json)) return new Dictionary<string, object>(); return ToObject<Dictionary<string, object>>(json); } /// <summary> /// 返回 List<Dictionary<string, object>> /// </summary> /// <param></param> /// <returns></returns> public static List<Dictionary<string, object>> ToList(string json) { if (String.IsNullOrEmpty(json)) return new List<Dictionary<string, object>>(); return ToObject<List<Dictionary<string, object>>>(json); } /// <summary> /// 组装对象 /// </summary> /// <param></param> /// <param></param> public static void PopulateObject(string json, object obj) { if (String.IsNullOrEmpty(json)) return; JsonConvert.PopulateObject(json, obj); } } }

7、用于区分系统异常和业务异常的BusinessException:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwgjy.html