Web Api 2 Session

大兔子大兔子 提交于 2019-12-20 09:55:36

问题


I cannot get session data from while in web api 2. I have verified that the cookie is sending in fiddler.

I know that web api 2 best practice is to be stateless, but due to requirements on a project it is now necessary to be state-full.

I have tried this link. WebAPI 2 attribute routing enable session state

var session = SessionStateUtility.GetHttpSessionStateFromContext(HttpContext.Current)

With the above solution I am getting a null exception on that function.

I also tried replicating the way of doing this the way you would in the old web api by modifying the requesthandler from the route, but that is not available in web api 2.

I currently set some session variables in mvc5. This works and the session stays, but anytime I am trying to use session while in web api 2 the below is null.

HttpContext.Current.Session

回答1:


Add

protected void Application_PostAuthorizeRequest() 
{
    System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}

to global.asax




回答2:


If PostAuthorizeRequest doesn't work, then try BeginRequest.



来源:https://stackoverflow.com/questions/21999409/web-api-2-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!