asp.net web api odataNo IdLink

hyp*_*erN 0 odata asp.net-web-api asp.net-web-api-odata

我正在使用带有web api的Odata并收到此错误:

 <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>
Run Code Online (Sandbox Code Playgroud)

这是我的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();
    }
Run Code Online (Sandbox Code Playgroud)

这是我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以告诉我,为什么我会收到此错误?

Kir*_*lla 6

你可能试图用ODataConventionModelBuilder而不是ODataModelBuilder.约定构建器将自动为您创建链接生成工厂.

因此,您可以更改以下代码:
var modelBuilder = new ODataConventionModelBuilder();

如果您对如何自己创建链接工厂感到好奇,可以查看以下示例(特别是文件ODataServiceSample/ODataService/ModelBuilder.cs):

http://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/ODataServiceSample/ReadMe.txt