I need to store some information in session(or in whatever in ASP.NET Web API) that I need to retrieve in every API request. We will have one api IIS web site and multiple web site binding will be added through host header. When any request comes in for example, api.xyz.com, host header will be checked and store that website information in session that will be used in each subsequent api request when making a call to database.
I know there is no support for session in ASP.NET Web API. Is there any other way to handle this kind of situation? Where can I store information that can be retrieving in each subsequent request?
thanks.
But in practice, yes - you may need to access a user's session from a web API. By default this is not possible.
In Asp, the session is cookie dependent . That is, Asp session only function when browser supports cookies. While Asp.Net supports cookieless session, so the session in Asp.Net is cookie independent .
Here is how you can do it. You can also use HTML5 Local Storage as well. Utilize HTML5 local storage store data into that and then use it. +1 for HTML5 Local Storage, which is probably the best place to store larges amounts of data for each client.
in Global.asax add
public override void Init() { this.PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest; base.Init(); } void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e) { System.Web.HttpContext.Current.SetSessionStateBehavior( SessionStateBehavior.Required); }
give it a shot ;)
Well, REST by design is stateless. By adding session (or anything else of that kind) you are making it stateful and defeating any purpose of having a RESTful API.
The whole idea of RESTful service is that every resource is uniquely addressable using a universal syntax for use in hypermedia links and each HTTP request should carry enough information by itself for its recipient to process it to be in complete harmony with the stateless nature of HTTP".
So whatever you are trying to do with Web API here, should most likely be re-architectured if you wish to have a RESTful API.
With that said, if you are still willing to go down that route, there is a hacky way of adding session to Web API, and it's been posted by Imran here http://forums.asp.net/t/1780385.aspx/1
Code (though I wouldn't really recommend that):
public class MyHttpControllerHandler : HttpControllerHandler, IRequiresSessionState { public MyHttpControllerHandler(RouteData routeData): base(routeData) { } } public class MyHttpControllerRouteHandler : HttpControllerRouteHandler { protected override IHttpHandler GetHttpHandler(RequestContext requestContext) { return new MyHttpControllerHandler(requestContext.RouteData); } } public class ValuesController : ApiController { public string GET(string input) { var session = HttpContext.Current.Session; if (session != null) { if (session["Time"] == null) { session["Time"] = DateTime.Now; } return "Session Time: " + session["Time"] + input; } return "Session is not availabe" + input; } }
and then add the HttpControllerHandler to your API route:
route.RouteHandler = new MyHttpControllerRouteHandler();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With