Monday, 18 July 2016

Adding multiple appSettings to webConfig file and getting value in ASP.MET MVC

Let's say we want to add few app settings file to a webconfig file, so if we have to change value of the key on the server, IIS does not have to recompile the whole application leading to better client/customer experience.

1. In webconfig add new section.


<section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089">

2. Create new config file in solution, let's call it CodeBasedOnReferer.config.
3. Add new key

    <add key="" value="Google_123"></add>

4. To obtain the code in aplication in controller:

NameValueCollection settings = (NameValueCollection) ConfigurationManager.GetSection("DatabaseConfig");
string code = settings (Request.UrlReferrer.Authority);

5. You can test your application using for instance fiddler. To create request open fiddler. Just in case you do not have it :

6. Your code should be Google_123.