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.

<configuration>
<configsections>

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


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

<DatabaseConfig>
    <add key="google.com" value="Google_123"></add>
</DatabaseConfig>

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 : http://www.telerik.com/fiddler


6. Your code should be Google_123.

Comments