2014-04-13 86 views
0

我必須在我的api中使用會話密鑰。會話密鑰是字符串值。 我必須得到並返回會話密鑰。 我創建SessionActionFilter,它將添加到響應會話密鑰。 但我不明白如何改變過濾器的響應。如何在odata中創建會話?

動作:

[SessionActionFilter] 
[Queryable] 
public IQueryable<Opportunity> GetOpportunity() 
{ 
    return db.Opportunities; 
} 

過濾:

public class SessionActionFilterAttribute : ActionFilterAttribute 
{ 
     public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) 
     { 
      //var sessionKey = ""; 
      //var oldObjectContent = (actionExecutedContext.ActionContext.Response.Content as ObjectContent); 
      //var newContent = new ObjectContent<Opportunity>(result, oldObjectContent.Formatter); 
      //actionExecutedContext.ActionContext.Response.Content = newContent; 

      base.OnActionExecuted(actionExecutedContext); 
     } 

} 

的響應數據:

{ 
     "odata.metadata":"http://localhost:2577/odata/$metadata#Opportunity", 
     "value":[ 
       { 
        "Id":4,"Name":"asda" 
       },{ 
        "Id":7,"Name":"s" 
       } ] 
} 

我所需要的響應數據

{ 
     "odata.metadata":"http://localhost:2577/odata/$metadata#Opportunity", 
     "sessionKey": "VALUE", 
     "value":[ 
       { 
        "Id":4,"Name":"asda" 
       },{ 
        "Id":7,"Name":"s" 
       } ] 
} 

回答