【发布时间】: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 => e.call_id == c_id)的末尾添加一个.ToList()...这对你有用吗? -
将
public IQueryable Get(int c_id)更改为public List<Call> Get(int c_id)。并将return entities.Calls.Where(e => e.call_id == c_id);更改为return entities.Calls.Where(e => e.call_id == c_id).ToList(); -
@ChetanRanpariya 谢谢,但我仍然遇到同样的错误。
-
你能在你的问题中写下你的 Call 类吗?
-
@Marlonchosky 当然!我现在就去做。
标签: c# .net asp.net-mvc asp.net-web-api controller