Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HttpContext.Current.Application to store simple data

I want to store a small list of a simple object (containing three strings) in my ASP.NET MVC application. The list is loaded from the database and it is updated rarely by editing some values in the site's admin area.

I'm thinking of using HttpContext.Current.Application to store it. This way I can load it in the Global.asax:

protected void Application_Start()
{
    RegisterRoutes(RouteTable.Routes);

    HttpContext.Current.Application["myObject"] = loadDataFromSql(); // returns my object
}

And then can easily reference it from any controllers or views as needed. Then in the event, the admin area calls the updateMyObject controller action, I can just update the DB and load it in again and replace HttpContext.Current.Application["myObject"].

Are there any cons to doing this? It seems like it will work fine for what I am trying to achieve, however does anyone know of a better way to do this, assuming there is some major disadvantage to the method I've laid out?

like image 736
macca1 Avatar asked Jun 21 '10 03:06

macca1


People also ask

What is use of HttpContext current?

What is HttpContext? It holds the current information about the Http request. It contains the information like authorization, authentication, request, response, session, items, users, formOptions, etc. Every HTTP request creates a new object of HttpContext with current information.

How use HttpContext current in .NET Core?

HttpContext parameter in middleware If you're writing custom middleware for the ASP.NET Core pipeline, the current request's HttpContext is passed into your Invoke method automatically: public Task Invoke(HttpContext context) { // Do something with the current HTTP context... }

Where is HttpContext stored?

It is stored in the memory of the server and the value is available for the entire lifetime of the request.

What is the use of HttpContext in asp net?

The HttpContext object constructed by the ASP.NET Core web server acts as a container for a single request. It stores the request and response information, such as the properties of request, request-related services, and any data to/from the request or errors, if there are any.


1 Answers

HttpContext.Current.Application is essentially a hangover that is needed for backwards compatibility with classic ASP. It's essentially a static Hashtable with classic ASP locking semantics (Application.Lock / Application.UnLock).

As a weakly-typed Hashtable, you will need to cast objects you retrieve:

MyObject myObject = (MyObject) HttpContext.Current.Application["myObject"];

In an ASP.NET application that is not a migration from classic ASP, I would prefer using other standard .NET stuff, such as:

  • A static field, using .NET locking semantics if you need locking (e.g. the C# lock keyword, or a ReaderWriterLockSlim instance, depending on your requirements):

    static MyObject myObject = LoadFromSql();

  • The ASP.NET Cache - which has rich functionality for managing expiration, dependencies, ...

like image 57
Joe Avatar answered Oct 22 '22 09:10

Joe