2013-07-09 59 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