You must Sign In to post a response.
  • Category: Webservices

    How to access Session in Web API MVC 5

    Dear All,

    I am unable to access session in WEB API , in our project, we have two solutions in our project as
    1. Web project
    2. WEB API project
    I want to access session from web project to WEB API project.

    Thanks & Regards,

    Atul Sharma
  • #766316
    If you are using WebAPi + MVC then you have do some changes in Global.ascx file to access session

    please go through above link

    Hope it will help you!

    /UMesh Bhosale

  • #766319

    you can go through below links


    Name : Dotnet Developer-2015
    Email Id

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766324
    First up all I would like to mention that 'By default HTTP/REST is stateless'. By adding/accessing session into your API, you are making it stateful and so you are overcoming the benefit of having a RESTful API applciation.
    So I recommend you to pass necessary parameters instead of using session variables.
    Now, if you are still willing to perform: then use the trick(s) as given in following URL:

  • #766350
    in Global.asax.cs file in MVC 5 use 'Application_PostAuthorizeRequest' event and call 'SetSessionStateBehavior' method see below snippet

    protected void Application_PostAuthorizeRequest()
    if (IsWebApiRequest())

    private bool IsWebApiRequest()
    return HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath.StartsWith(WebApiConfig.UrlPrefixRelative);

    see below URL for more details

    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

Sign In to post your comments