Manuel Valle
Manuel Valle

Reputation: 297

ASP.NET MVC Web Api 4.5 Session state

I found solutions to add Session State for web api 4.0. But I have not found one for 4.5. Could some one point how to accomplish this?

Upvotes: 0

Views: 4112

Answers (2)

Stumblor
Stumblor

Reputation: 1140

You can test the incoming request using RouteTable.Routes.GetRouteData to determine whether it is an Web API request:

    protected void Application_PostAuthorizeRequest()
    {
        // WebApi SessionState
        var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));
        if (routeData != null && routeData.RouteHandler is HttpControllerRouteHandler)
            HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
    }

Upvotes: 3

Michael
Michael

Reputation: 31

Use this solutions:

But instead of the following code in de webapiconfig

var route = config.Routes.MapHttpRoute(...

use the RoutTable class

var route = RouteTable.Routes.MapHttpRoute(...

Upvotes: 3

Related Questions