1、统一JSON格式处理方式,同时指定ContentType类型,解决低版本浏览器获取json时ContentType为application/json提示下载的问题.
public abstract class CustomResult : ActionResult
{
public abstract TData GetObject();
protected JsonSerializerSettings SerializerSettings;
protected void InitSerialization(ControllerContext context)
{
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "text/html";
TData data = GetObject();
if (SerializerSettings == null)
{
SetSerializerSettings();
}
response.Write(JsonConvert.SerializeObject(data, Formatting.None, SerializerSettings));
}
protected virtual void SetSerializerSettings()
{
SerializerSettings = new JsonSerializerSettings
{
Converters = new List
{
new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd hh:mm" }
}
};
}
public override void ExecuteResult(ControllerContext context)
{
InitSerialization(context);
}
}
Original: https://www.cnblogs.com/zhongkai/p/4033491.html
Author: OneWork
Title: ASP.NET MVC 自定义处理JSON ActionResult类
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/572986/
转载文章受原作者版权保护。转载请注明原作者出处!