2013-07-01 59 views
2

我正在開發一個使用Web Api和ODataController的OData查詢庫。當我從Web瀏覽器運行我的api時,它什麼都不返回。我沒有收到任何類型的錯誤。我可以在Visual Studio中進行調試,並清楚地看到該方法運行併成功將我的結果返回爲IQueryable <>。在引擎蓋下的某處它丟棄了我的數據。有沒有其他人看到或遇到過這個?我已經包括了我的代碼如下,僅供參考:Web Api OData控制器返回空白頁無錯誤

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using System.Web.Http.OData; 
using Epm.Core.Model; 
using System.Web.Http.OData.Query; 
using Epm.Data.Access; 
using Epm.Service.Assemblers; 

namespace Epm.Service.Web.Controllers.OData 
{ 
    public class SearchActionsController : ODataController 
    { 
     private readonly EpmEntities context = new EpmEntities(); 

     [Queryable(AllowedQueryOptions=AllowedQueryOptions.All)] 
     public IQueryable<ActionStepDisplay> Get(int planId, int? factorId, bool? showArchived) 
     { 
      var user = this.GetCurrentUser(); 

      var results = (from p in context.SearchActions(user.SessionId, planId, factorId, showArchived, 1, null) 
          select p).ToModel().ToArray(); 

      return results.AsQueryable(); 
     } 

     protected override void Dispose(bool disposing) 
     { 
      context.Dispose(); 
      base.Dispose(disposing); 
     } 
    } 
} 

我的配置:

ODataModelBuilder modelBuilder = new ODataConventionModelBuilder(); 
modelBuilder.EntitySet<Epm.Core.Model.ActionStep>("SearchActions"); 

Microsoft.Data.Edm.IEdmModel model = modelBuilder.GetEdmModel(); 
config.Routes.MapODataRoute("ODataRoute", "odata", model); 
+0

嘗試運行在小提琴手查詢不接受(406),看看是否有響應的任何錯誤。 –

回答

0

可能問題出在MediaFormatter,控制器完成後調用。當媒體類型格式化程序遇到參考循環(其中對象A引用B和B引用A)時,則需要告訴媒體類型格式化程序如何處理它,因此在Json媒體類型格式化程序中,您可以執行類似操作...

json.SerializerSettings.PreserveReferencesHandling = 
    Newtonsoft.Json.PreserveReferencesHandling.All; 

Documentation Here

我建議你使用Fiddler看看什麼是真正回事。你說你沒有在瀏覽器中得到響應,那麼返回什麼HTTP代碼?您可以使用Fiddler找出...

3

你是從你的方法返回ActionStepDisplay,但建設者指定ActionStep作爲實體。

可能是在響應頭