Reputation: 493
I need to run some code that will fetch some configuration values from the web.config during first run of an asp.net mvc application. These values will not change frequently but that is not my main concern.
One way that I can think of is calling the method in Application_Start() method in the global.asax.cs file, but I am hoping someone has a better idea.
Upvotes: 2
Views: 7912
Reputation: 115498
Application start was really created for purposes like that. If you are just populating things from the Web.Config why not create a class that pulls them directly from there if you are worried about abstraction? It's already cached by ASP.Net, so you aren't paying a penalty from accessing the web.config multiple times. This way you don't have to worry about using the Global.Asax which you seem to be against.
If you are really against using the Global.Asax, you could always have a method that checks to see if they are loaded on the landing page on your site, or have a method in the master page that fires each time a page using it is accessed. I would still use the Global.asax Application_Start or Session_Start myself though.
Upvotes: 7
Reputation: 57939
I think that is pretty standard.
(Not a link to what I'd call a "recommended approach," just something that demonstrates that Application_Start is the jumping off point for all sorts of configuration and initialization.)
Upvotes: 0
Reputation: 38503
That is what I would recommend, do you have something against that?
If you explain a little more about what you are trying to accomplish, we may be able to help more.
Upvotes: 0