Friday 23 March 2012

MVC 4 Web API access Session

It's not recommended to use Session  in Web API for various of good reasons. However, in case you're still so interested to access Session for any business needs, here is a quick solution to allow accessing Session in Web API.

 
// In global.asax
public class MvcApp : System.Web.HttpApplication
{
 public static void RegisterRoutes(RouteCollection routes)
 {
  var route = routes.MapHttpRoute(
   name: "DefaultApi",
   routeTemplate: "api/{controller}/{id}",
   defaults: new { id = RouteParameter.Optional }
  );
  route.RouteHandler = new MyHttpControllerRouteHandler();
 }
}

// Create two new classes
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);
 }
}

// Now Session is visible in your Web API
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;
 }
}
 



Source is here.

Thursday 22 March 2012

First Cloud OS - Solaris 11

Oracle has released Solaris 11 and claimed it as the first Cloud OS in the market. Looking forward to having my hands on it.

Details of Oracle Solaris 11 can be found on here