错误'ObjectContent`1'类型无法序列化内容类型'application / json的响应主体;字符集= UTF-8'

时间:2016-06-30 10:59:50

标签: asp.net-mvc-5 asp.net-web-api2

我正在编写一个web api来从数据库中获取所有用户,但它会出现此错误 The 'ObjectContent 1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.我已经添加了webapiconfig.cs文件和web api的所有先决条件。

的WebAPI

using Atea.Azure.ApiManagement.Entities;
using Atea.Azure.ApiMangement.Business;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Azure_API_Delegation_Portal.Controllers
{
    public class ApisController : ApiController
    {
        private readonly IUserService _userService;

        public ApisController(IUserService userService)
        {
            _userService = userService;

        }

        // GET api/<controller>
        public IEnumerable<User> GetAllUsers()
        {
            return _userService.AllUser();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

这应该这样做:

public JsonResult GetAllUsers()

{
   return Json(_userService.AllUser(), JsonRequestBehavior.AllowGet)

}