实现 IActionResult , 只需要记住 Response 有一个 WriteAsync 扩展方法即可

    public class JsonResult<T> : IActionResult
    {
        private T _data;

        public JsonResult(T data)
        {
            _data = data;
        } 

        public Task ExecuteResultAsync(ActionContext context)
        {
            HttpResponse response = context.HttpContext.Response;
            response.ContentType = $"{context.HttpContext.Request.ContentType}; charset=utf-8";

            string json = string.Empty;
            if (this._data != null)
            {
                json = JsonConvert.SerializeObject(_data);
            }

            return Task.FromResult(response.WriteAsync(json));
        }
    }

相关文章:

  • 2021-08-30
  • 2021-07-24
  • 2021-08-16
  • 2021-06-27
  • 2021-08-31
  • 2021-09-16
猜你喜欢
  • 2022-01-09
  • 2021-11-11
  • 2021-07-16
  • 2022-12-23
  • 2021-07-18
  • 2021-11-04
相关资源
相似解决方案