我正在编写一个web api来从数据库中获取所有用户,但它会出现此错误
The 'ObjectContent 1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.
我已经添加了webapiconfig.cs文件和web api的所有先决条件。
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();
}
}
}
答案 0 :(得分:1)
这应该这样做:
public JsonResult GetAllUsers()
{
return Json(_userService.AllUser(), JsonRequestBehavior.AllowGet)
}