1、统一JSON格式处理方式,同时指定ContentType类型,解决低版本浏览器获取json时ContentType为application/json提示下载的问题.

 public abstract class CustomResult<TData> : 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<JsonConverter>
                {
                    new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd hh:mm" }
                }
            };
        }

        public override void ExecuteResult(ControllerContext context)
        {
            InitSerialization(context);
        }
    }

 

  

相关文章:

  • 2021-10-24
  • 2021-09-06
  • 2022-12-23
  • 2018-04-20
  • 2022-01-03
  • 2021-10-16
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2019-10-25
  • 2022-01-14
  • 2021-12-07
  • 2021-07-07
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
相关资源
相似解决方案