Search code examples
c#asp.net-mvcconfigurationhttpcontext

WebConfigurationManager or HttpContext.Application?


I'm a beginner with C#, .net etc., and I am working with a MVC Web Application.

I saw some examples where, in Application_Start() in Global.asax.cs, people use Application.Add("prop", "value") to store values from Web.config.

Then we can access it in the controllers, through HttpContext.Application["prop"].

Is there a difference between using

HttpContext.Application["prop"]

and using

WebConfigurationManager["prop"]

?

I think in this page: http://msdn.microsoft.com/en-us/library/system.web.configuration.webconfigurationmanager.aspx it is advised to use WebConfigurationManager for Web applications, but they don't talk about HttpContext.Application.

Thanks a lot!


Solution

  • Both have different purposes.

    The HttpContext.Application represents HttpApplicationState, which you can think about as a set of application level global variables. It is stored in-memory and not persisted to disk, so it's lost if the worker process is restarted.

    On the other hand, the WebConfigurationManager is used to access the Web.Config file. It is stored on disk and persisted when the app pool is recycled.

    For the particular case you are describing, I suppose someone may have thought that there was a performance benefit in loading the properties from Web.Config into memory and accessing them from memory, although I'm not convinced that much of a performance benefit will be achieved.