rest webapi 返回数据

webapi可以直接返回一个对象,也可以返回json

一、返回一个对象例子

[System.Web.Mvc.AllowAnonymous]
        [System.Web.Http.HttpGet]
        public ApprovalInfoCounts ApprovalCount(string userId)
        {
            ApprovalInfoCounts infoCounts = new ApprovalInfoCounts();
}

 

二、返回一个json帮助类

public class ApiResponseHelper
     {
         public static HttpResponseMessage ToJson(Object obj)
         {
             String str;
             if (obj is String || obj is Char)
             {
                 str = obj.ToString();
             }
             else
             {
                 JavaScriptSerializer serializer = new JavaScriptSerializer();
                 str = serializer.Serialize(obj);
             }
             HttpResponseMessage result = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") };
             return result;
         }
     }

 

posted @ 2019-01-14 20:38  爱生活,爱代码  阅读(536)  评论(0编辑  收藏  举报