我将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.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>下面是我的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();
}这是我的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);
}
}谁能告诉我,为什么我会得到这个错误?
发布于 2013-07-10 01:28:18
您可能正在尝试使用ODataConventionModelBuilder而不是ODataModelBuilder。约定构建器将自动为您创建链接生成工厂。
因此,您可以更改代码,如下所示:
var modelBuilder = new ODataConventionModelBuilder();
如果您对如何自己创建链接工厂感到好奇,可以查看以下示例(特别是文件ODataServiceSample/ODataService/ModelBuilder.cs):
http://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/ODataServiceSample/ReadMe.txt
https://stackoverflow.com/questions/17554326
复制相似问题