'ObjectContent`1':无法序列化来自DbSet的响应 [英] 'ObjectContent`1': Failed to serialize the response from DbSet
本文介绍了'ObjectContent`1':无法序列化来自DbSet的响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在TabletController.cs
文件中具有以下控制器:
I have the following controller in TabletController.cs
file:
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,但出现相同的错误.
Also, I need to get the results of the table's fields in json
format. I tried to serialize it in JSON, but I get the same error.
<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 }
);
}
}
推荐答案
我找到了解决方案.
我已将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));
}
}
这篇关于'ObjectContent`1':无法序列化来自DbSet的响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文