如何在odata中创建会话

本文关键字:创建 会话 odata | 更新日期: 2023-09-27 17:58:02

我必须在我的api中使用会话密钥。会话密钥是字符串值。我必须获取并返回会话密钥。我创建了SessionActionFilter,它将添加到响应会话密钥中。但我不明白过滤器中的变化是如何响应的。

作用:

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

a响应数据:

{   
        "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"
                }   ] 
}

如何在odata中创建会话

您需要创建一个自定义ODataEntityTypeSerializer,只需遵循以下示例:https://aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/v3/CustomODataFormatter/