当前位置:   article > 正文

.NET MVC API返回JSON对象

.NET MVC API返回JSON对象

方法多种,自己目前采用的是自定义返回格式的方法,不需要修改配置文件。

辅助类:

 1 public class ApiResponseHelper
 2     {
 3         public static HttpResponseMessage ToJson(Object obj)
 4         {
 5             String str;
 6             if (obj is String || obj is Char)
 7             {
 8                 str = obj.ToString();
 9             }
10             else
11             {
12                 JavaScriptSerializer serializer = new JavaScriptSerializer();
13                 str = serializer.Serialize(obj);
14             }
15             HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
16             return result;
17         }
18     }

调用:

[LoginFilter(IsCheck = false)]
        [HttpGet]
        public object Audit(int id, int status)
        {
            try
            {
                auditSvc.BulkUpdateByQuery(c => c.ID == id, d => new HXAudit { AuditStatus = status, AuditTime = DateTime.Now });
                return ApiResponseHelper.ToJson(new { status = true, message = "成功", data = new { } });
            }
            catch (Exception e)
            {
                return ApiResponseHelper.ToJson(new { status = false, message = e.Message, data = new { } });
            }
        }

 

转载于:https://www.cnblogs.com/toloe/p/8026972.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/550321
推荐阅读
相关标签
  

闽ICP备14008679号