Webapi mvc session cannot be obtained all the time. webapimvc

Source: Internet
Author: User

Webapi mvc session cannot be obtained all the time. webapimvc

Some time ago, when I wrote an interface to a mobile client, I encountered a problem that the mailbox session cannot be obtained by calling the interface. Let me talk to the comrades who have encountered problems.

I checked a lot of information on the Internet and asked some friends. Finally, the solution is found.

 

We all know that webapi does not enable session support by default. Therefore, you need to rewrite the Global file as follows:

Public override void Init ()
{
This. PostAuthenticateRequest + = (sender, e) => HttpContext. Current. SetSessionStateBehavior (SessionStateBehavior. Required );
Base. Init ();
}

Now you can store sessions in the api controller you created,

HttpContext. Current. Session ["a"] = "1111 ";

However, when your mailbox is sent out. When you open the mailbox page for verification, you find that the obtained session is null. Here I want to explain it.

During verification, please use JavaScript asynchronous processing. Go to the controller where you set the session and write an interface method. Ajax call.

You can get the session in the corresponding controller.

 

This is my temporary solution. I don't know if there is any better way to do this. Learn from each other.

 

-----------------------------------------------------------------------

 

Related Article

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.