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
    Hi
    If you are using WebAPi + MVC then you have do some changes in Global.ascx file to access session

    http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api

    please go through above link

    Hope it will help you!

    Thanks
    /UMesh Bhosale

  • #766319
    Hi

    you can go through below links


    "code.msdn.microsoft.com/How-to-create-and-access-447ada98"
    "stackoverflow.com/questions/14138872/how-to-use-sessions-in-an-asp-net-mvc-4-application"

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

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

  • #766324
    Hi,
    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:
    http://www.codeproject.com/Tips/513522/Providing-session-state-in-ASP-NET-WebAPI

  • #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())
    {
    HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
    }
    }

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

    see below URL for more details
    http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api

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


Sign In to post your comments