简体   繁体   中英

How create session in odata?

I have to use session key in my api. A session key is string value. I have to get and return session key. I create SessionActionFilter which will should add to response session key. But I don't understand how change response in filter.

ACtion:

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

Filter:

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 response data:

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

I need the response data

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM