Create two classes; SessionControllerHandler and SessionHttpControllerRouteHandler. Implement as follows:
public
class
SessionControllerHandler : HttpControllerHandler, IRequiresSessionState
{
public
SessionControllerHandler(RouteData routeData)
:
base
(routeData)
{ }
}
public
class
SessionHttpControllerRouteHandler : HttpControllerRouteHandler
{
protected
override
IHttpHandler GetHttpHandler(RequestContext requestContext)
{
return
new
SessionControllerHandler(requestContext.RouteData);
}
}
In your WebApiConfig, add the following above your route declaration(s):
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var httpControllerRouteHandler = typeof(HttpControllerRouteHandler).GetField("_instance",
System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
if (httpControllerRouteHandler != null)
{
httpControllerRouteHandler.SetValue(null,
new Lazy<HttpControllerRouteHandler>(() => new SessionHttpControllerRouteHandler(), true));
}
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Now Session is On
HttpContext.Current.Session["ForLeftMenu"] = "Rajon";
if (HttpContext.Current.Session["ForLeftMenu"] != null)
{
string text = HttpContext.Current.Session["ForLeftMenu"].ToString();
}
No comments:
Post a Comment