2013-07-09 38 views
0

我使用的OData通过Web API和得到这个错误:asp.net网页API odataNo IdLink

<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); 
    } 
} 

任何人都可以请告诉我,为什么我得到这个错误?

回答

6

您可能试图使用ODataConventionModelBuilder而不是。约定生成器会自动为您创建链接生成工厂。

所以,你可以改变像下面的代码:
var modelBuilder = new ODataConventionModelBuilder();

,如果你是好奇,怎么自己创建的链接工厂,你可以检查以下样品(特别是文件ODataServiceSample/ODataService/ModelBuilder.cs):

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

+0

它为我工作,因为我有一个集合,我需要URI查询。 – Maxymus