Asp.net web api asp.net web api odataNo IdLink
我将Odata与web api一起使用,出现以下错误:Asp.net web api asp.net web api odataNo IdLink,asp.net-web-api,odata,asp.net-web-api-odata,Asp.net Web Api,Odata,Asp.net Web Api Odata,我将Odata与web api一起使用,出现以下错误: <m:innererror> <m:message> The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'. </m:message> <m:type>System.InvalidOperationExc
<m:innererror>
<m:message>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.
</m:message>
<m:type>System.InvalidOperationException</m:type>
<m:stacktrace/>
<m:internalexception>
<m:message>
No IdLink factory was found. Try calling HasIdLink on the EntitySetConfiguration for 'Tag'.
</m:message>
<m:type>System.InvalidOperationException</m:type>
'ObjectContent'1'类型未能序列化'application/json'内容类型的响应正文;字符集=utf-8'。
System.InvalidOperationException异常
没有发现IdLink工厂。尝试在EntitySetConfiguration上为“标记”调用HasIdLink。
System.InvalidOperationException异常
以下是我的OData配置:
config.Routes.MapODataRoute("ODataRoute", "odata", CreateModel());
static IEdmModel CreateModel()
{
var modelBuilder = new ODataModelBuilder();
modelBuilder.EntitySet<Tag>("Tag");
modelBuilder.EntitySet<Post>("Post");
return modelBuilder.GetEdmModel();
}
config.Routes.MapODataRoute(“ODataRoute”,“odata”,CreateModel());
静态IEdmModel CreateModel()
{
var modelBuilder=new ODataModelBuilder();
modelBuilder.EntitySet(“标记”);
modelBuilder.EntitySet(“Post”);
返回modelBuilder.GetEdmModel();
}
这是我的OData控制器
public class TagController : EntitySetController<Tag, int>
{
private readonly IUnitOfWork _unitOfWork;
public TagController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public override IQueryable<Tag> Get()
{
return _unitOfWork.BlogTagQueries.GetAllTags().AsQueryable();
}
protected override Tag GetEntityByKey(int key)
{
return _unitOfWork.BlogTagQueries.GetTagById(key);
}
}
公共类TagController:EntitySetController
{
私人只读i工作单元(unitof工作单元);;
公共标记控制器(IUnitOfWork unitOfWork)
{
_unitOfWork=unitOfWork;
}
公共覆盖IQueryable Get()
{
返回_unitOfWork.blogtagquerys.GetAllTags().AsQueryable();
}
受保护的覆盖标记GetEntityByKey(int键)
{
返回_unitOfWork.blogtagquerys.GetTagById(键);
}
}
谁能告诉我,为什么我会犯这个错误 您可能试图使用
ODataConventionModelBuilder
而不是ODataModelBuilder
。约定生成器将自动为您创建链接生成工厂
因此,您可以按如下方式更改代码:var modelBuilder=new ODataConventionModelBuilder()代码>
如果您想知道如何自己创建链接工厂,可以查看以下示例(特别是文件odataservicecsample/ODataService/ModelBuilder.cs
):
您可能试图使用ODataConventionModelBuilder
而不是ODataModelBuilder
。约定生成器将自动为您创建链接生成工厂
因此,您可以按如下方式更改代码:
var modelBuilder=new ODataConventionModelBuilder()代码>
如果您想知道如何自己创建链接工厂,可以查看以下示例(特别是文件odataservicecsample/ODataService/ModelBuilder.cs
):
它对我起作用,因为我有一个需要URI查询的集合。它对我起作用,因为我有一个需要URI查询的集合。