网页API 2届 [英] Web Api 2 Session
问题描述
我无法从而在网页API 2.我已经验证该cookie被发送小提琴手获取会话数据。
我知道,网页API 2最好的做法是是无状态的,但由于对项目的要求,现在有必要为全状态。
我已经试过此链接。
<一href=\"http://stackoverflow.com/questions/19758221/webapi-2-attribute-routing-enable-session-state\">WebAPI 2属性路由的会话状态
VAR会话= SessionStateUtility.GetHttpSessionStateFromContext(HttpContext.Current)
通过上述方案,我越来越对这个函数空例外。
我也试过复制这样做,通过修改从路线requesthandler您在旧的Web API会的方式的方式,但不是在网页API提供2
我目前设置mvc5一些会话变量。这工作和会话保持,但只要我想使用会话而在网页API 2以下为null。
HttpContext.Current.Session
添加
保护无效Application_PostAuthorizeRequest()
{
System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}
要Global.asax中
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
Add
protected void Application_PostAuthorizeRequest()
{
System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}
to global.asax
这篇关于网页API 2届的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!