Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Web API session or something?

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.

like image 813
user1186065 Avatar asked Jul 13 '12 20:07

user1186065


People also ask

Can Web API have session?

But in practice, yes - you may need to access a user's session from a web API. By default this is not possible.

What is the difference between ASP session and ASP.NET session?

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 .

How does Web API store data in session?

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.


2 Answers

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 ;)

like image 54
Soren Avatar answered Oct 03 '22 14:10

Soren


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(); 
like image 23
Filip W Avatar answered Oct 03 '22 15:10

Filip W