直接贴代码了:
using System; using System.Web.Mvc; using Newtonsoft.Json; namespace MvcSample.Extensions { public class ConverterJsonResult : JsonResult { #region Fields private readonly JsonConverter[] _converters; #endregion #region Ctor public ConverterJsonResult(params JsonConverter[] converters) { _converters = converters; } #endregion #region Methods public override void ExecuteResult(ControllerContext context) { if (context == null) throw new ArgumentNullException("context"); if (context.HttpContext == null || context.HttpContext.Response == null) return; context.HttpContext.Response.ContentType = !string.IsNullOrEmpty(ContentType) ? ContentType : "application/json"; if (ContentEncoding != null) context.HttpContext.Response.ContentEncoding = ContentEncoding; if (Data != null) context.HttpContext.Response.Write(JsonConvert.SerializeObject(Data, _converters)); } #endregion } }
谢谢浏览!