Web API Open Session Support

Source: Internet
Author: User

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.

    • at this time to add the following code in the Global.asax to open the session function (override the Init method)

<summary>///Open Session in WEBAPI Support//</summary>public override void Init () {this. Postauthenticaterequest + = (sender, E) = HttpContext.Current.SetSessionStateBehavior ( sessionstatebehavior.required); base. Init ();}
    • Add the following code to determine if it is WEBAPI, then open the session function

<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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.