【问题标题】:'ObjectContent`1': Failed to serialize the response from DbSet'ObjectContent`1':无法序列化来自 DbSet 的响应
【发布时间】:2020-05-07 21:18:48
【问题描述】:

我在TabletController.cs 文件中有以下控制器:

public class TabletController : ApiController
{
    public IQueryable Get(int c_id)
    {
        using (EMSMVCEntities entities = new EMSMVCEntities())
        {
            return entities.Calls.Where(e => e.call_id == c_id); 
        }
    }
}

我正在打电话:

http://localhost:53366/api/Tablet/157

但我收到以下错误:

Call是dDbSet

另外,我需要以json 格式获取表格字段的结果。我试图用 JSON 序列化它,但我得到了同样的错误。

<ExceptionMessage>
Type 'System.Data.Entity.Infrastructure.DbQuery`1[[EMSMVC.Models.Call, EMSMVC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ArrayOfCall:http://schemas.datacontract.org/2004/07/EMSMVC.Models' is not expected. Consider using a DataContractResolver if you are using DataContractSerializer or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to the serializer.
</ExceptionMessage>
<ExceptionType>
System.Runtime.Serialization.SerializationException
</ExceptionType>

我的WebApiConfig.cs 文件是:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "TabletCallApi",
            routeTemplate: "api/{controller}/{c_id}",
            defaults: new { controller = "Tablet", action = "Get" }
            );

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

另外,我的RouteConfig.cs 是:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional }
        );
    }
}

【问题讨论】:

  • 尝试在entities.Calls.Where(e =&gt; e.call_id == c_id) 的末尾添加一个.ToList() ...这对你有用吗?
  • 将 public IQueryable Get(int c_id) 更改为 public List&lt;Call&gt; Get(int c_id)。并将return entities.Calls.Where(e =&gt; e.call_id == c_id); 更改为return entities.Calls.Where(e =&gt; e.call_id == c_id).ToList();
  • @ChetanRanpariya 谢谢,但我仍然遇到同样的错误。
  • 你能在你的问题中写下你的 Call 类吗?
  • @Marlonchosky 当然!我现在就去做。

标签: c# .net asp.net-mvc asp.net-web-api controller


【解决方案1】:

我找到了解决办法。

我已将TableController.cs 文件更改为

public System.Web.Http.Results.JsonResult<Call> Get(int c_id)
    {
        using (EMSMVCEntities entities = new EMSMVCEntities())
        {
            entities.Configuration.ProxyCreationEnabled = false;
            return Json(entities.Calls.FirstOrDefault(e => e.call_id == c_id));
        }
    }

【讨论】:

    猜你喜欢
    • 2013-08-10
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 2016-09-15
    • 1970-01-01
    • 2012-10-07
    • 1970-01-01
    • 2015-06-27
    相关资源
    最近更新 更多