【问题标题】:Error The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'错误“ObjectContent`1”类型无法序列化内容类型“application/json”的响应正文;字符集=utf-8'
【发布时间】:2016-06-30 10:59:50
【问题描述】:

我正在编写一个 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();
        }
    }
}

【问题讨论】:

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


    【解决方案1】:

    应该这样做:

    public JsonResult GetAllUsers()
    
    {
       return Json(_userService.AllUser(), JsonRequestBehavior.AllowGet)
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-27
      • 2012-12-07
      • 2017-07-17
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 2013-10-01
      • 2012-10-07
      • 2020-02-28
      相关资源
      最近更新 更多