The Web API in the default is not open session support, open session support, need to do the following, the following operations are divided into two kinds, are in the Global.asax file to add code.
<summary>///Open Session in WEBAPI Support//</summary>public override void Init () {this. Postauthenticaterequest + = (sender, E) = HttpContext.Current.SetSessionStateBehavior ( sessionstatebehavior.required); base. Init ();}
<summary>///determine if a Web API request///</summary>///<returns></returns>private bool Iswebapirequest () {///based on the virtual path of the application root to determine if the API program return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePa Th. ToLower (). StartsWith (Webapiexecutepath);} <summary>///if it is an API, open session///</summary>protected void Application_postauthorizerequest () {if ( Iswebapirequest ()) {HttpContext.Current.SetSessionStateBehavior (System.Web.SessionState.SessionStateBehavior. Required); }}
At the same time in the WEBAPI test session of the write is correct, note that the session to write and read to use the same browser or tool, I in the process of testing, I made a low-level error, the session has been written, but has not been obtained, Just because I was logged in through the browser to write the session, but in the postman to get the session, but has not been obtained.
This article is from the "Why A Smile" blog, please be sure to keep this source http://helicon.blog.51cto.com/3926609/1912142
Web API Open Session Support