ASP.NET MVC 下自定义 JsonResult,使用 Json.NET 序列化 JSON
2018-12-19 18:21 音乐让我说 阅读(1424) 评论(0) 收藏 举报直接贴代码了:
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 } }
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
【推荐】100%开源!大型工业跨平台软件C++源码提供,建模,组态!
【推荐】AI 的力量,开发者的翅膀:欢迎使用 AI 原生开发工具 TRAE
【推荐】2025 HarmonyOS 鸿蒙创新赛正式启动,百万大奖等你挑战
2011-12-19 调用百度“搜索建议(BaiduSuggestion)”的 API
2011-12-19 定义一个 GlobalSqlConfigHelper 类,实现读取 XML 文件中的 SQL 语句。
2011-12-19 关于 C# 中的 volatile 关键字