0

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!

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Jo Pango
  • 474
  • 1
  • 5
  • 15

2 Answers2

1

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.

Zaid Masud
  • 13,225
  • 9
  • 67
  • 88
0

When you use the HttpApplicationState to store and retrieve values, no writing to the web.config file is done - indeed, it's likely already been loaded for that request anyway. What you're doing is storing it in some form of cache that exists as long as the lifetime of the application.

With a configuration manager, that will write to the underlying configuration file (of that instance) when a call to Save is made.

Basically, these are different things although they could seem to function to you, the user, in the same way.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129