Skip to main content

Posts

Showing posts with the label How To Access Session Variables in Web API 2 Controller in ASP.Net MVC 5?

How To Access Session Variables in Web API 2 Controller in ASP.Net MVC 5?

Accessing Session Using ASP.NET Web API 2 in MVC 5 - Well, as you know, REST API by design is stateless. By adding session variables you are making it stateful and defeating any purpose of having a RESTful API . In WebApi 2 you can add this to Global.asax – //Global.asax protected void Application_PostAuthorizeRequest () {    System . Web . HttpContext . Current . SetSessionStateBehavior ( System . Web . SessionState . SessionStateBehavior . Required ); } Then you could access the session through – namespace demo . Api {     [ EnableCors ( origins : "http://localhost:53865,https://app.code-sample.com" , headers : "*" , methods : "*" )]     public class BaseAPIController : ApiController     {         /// < summary >         /// BaseAPIController constructor will check that identity exists or not.   ...