Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add custom class to HttpContext

In a ASP.NET MVC C# project:

I’m currently using HttpContext.Items to store a property that should be used everywhere in that request. This is what I have in the controller:

HttpContext.Items["IUser"] = new CustomClass(“parameter”);

This works fine to retrieve in the view since I’m overriding the default WebViewPage like this in the views web.config file:

<pages pageBaseType="Project.CustomWebViewPage">

Inside the CustomWebViewPage I create a property and set it in the InitHelpers() method like this:

IUser = (CustomClass)HttpContext.Current.Items["IUser"];

However, if I want to retrieve it in my Models/ViewModels I will have to put that code piece of code everywhere, and therefore I thought of instead creating the CustomClass directly inside the HttpContext (so I at least won’t have to typecasting), but I’m unable to find how to do override or dynamically add that object to it.

like image 801
Johan Svensson Avatar asked May 29 '13 09:05

Johan Svensson


People also ask

What is the HttpContext class?

The HttpContext encapsulates all the HTTP-specific information about a single HTTP request. When an HTTP request arrives at the server, the server processes the request and builds an HttpContext object. This object represents the request which your application code can use to create the response.

Is HttpContext current thread safe?

HttpContext is not thread safe! No deadlocks if you block a Task with Task.

When should I use HttpContext items?

Items to store the data. Similarly, you use HTTPContext Items collection when you are sharing the same information across the different instance based on the user request and that request could be changed for a different request.


2 Answers

You could create an extension method. Something like this:

public static class HttpContextExtensions
{
    public static CustomClass GetUser(this HttpContextBase context)
    {
        return (CustomClass)context.Items["IUser"];
    }
}

Then you would call it like this:

var user = HttpContext.Current.GetUser();
like image 166
levelnis Avatar answered Nov 07 '22 23:11

levelnis


I recently faced a similar problem. While I've worked with WebForms, my solution should also apply to MVC.

I created a new object to access the DAL/Business Logic layers for each request. This enables me to perform changes to the logic, no matter of how long the request takes, or if there are multiple requests executed concurrently. Also it allowed ensure my object's disposal when the request ended.

My first thought was to create a custom implementation of HttpContextBase, but I couldn't find any way to overwrite the creation of the HttpContext for the request.

So finally I introduced an extension method that encapsulates all the logic:

public static class HttpContextExtensions
{
    public static CustomClass GetIUser(this HttpContextBase Context)
    {
        return Context.Items["IUser"] as CustomClass;
    }
}

Now your code simplifies to:

var user = Request.RequestContext.HttpContext.GetIUser();
like image 41
Carsten Avatar answered Nov 07 '22 22:11

Carsten