0

I have MVC5 .NET 4.6.1 C# web application I want to create a custom config file separate from web.config to store some settings my application uses.

I tried to follow this article https://support.microsoft.com/en-us/kb/815786

however the items I set in app.config:

<?xml version="1.0"?>
<configuration>

    <system.web>
      <compilation debug="true" targetFramework="4.6.1" />
      <httpRuntime targetFramework="4.6.1" />
    </system.web>
  <appSettings>
      <add key="Key0" value="0" />
      <add key="Key1" value="1" />
      <add key="Key2" value="2" />
   </appSettings>

</configuration>

are not seen in my application see , eg. they come as null:

string attr = ConfigurationManager.AppSettings["Key0"];

Why isn't it working? Am I missing something?

Alternatively I would like to create a custom config file eg. mycustom.config to define my global app settings.

EDIT

Solution I used

Follwing this post https://social.msdn.microsoft.com/Forums/vstudio/en-US/11e6d326-c32c-46b1-a9a2-1fbef96f33ee/howto-custom-configuration-files?forum=netfxbcl

In web.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
       <configSections>
              <section name="newAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
       </configSections>
       <newAppSettings file="C:\mycustom.config"/>
</configuration>

Then mycustom.config

<?xml version="1.0" encoding="utf-8" ?>
<newAppSettings>
       <add key="OurKey" value="OurValue"/>
</newAppSettings>

And reading the value:

System.Collections.Specialized.NameValueCollection newAppSettings = (System.Collections.Specialized.NameValueCollection)System.Configuration.ConfigurationManager.GetSection("newAppSettings");
string key = Convert.ToDateTime(newAppSettings["OurKey"]);

2 Answers 2

3

You can use separate config file for connection strings and app settings:

<appSettings configSource="appSettings.config" />
<connectionStrings configSource="connectionStrings.config"/>

appSettings.config file

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="Setting1" value="App setting 1" />
</appSettings>

connectionStrings.config file

<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
    <add name="MyConnStr1" connectionString="My connection string" />
</connectionStrings>

Usage is same as it was before:

var setting1 = ConfigurationManager.AppSettings["Setting1"];
var connString1 = ConfigurationManager.ConnectionStrings["MyConnStr1"].ConnectionString;
Sign up to request clarification or add additional context in comments.

1 Comment

Not exactly what I was looking for but useful as well, I didn't try it's working or not though - i assume it does. I used slightly different approach which is more convenient/ personalised for my application - see my edit. Thanks for help
-1
            //Helps to open the Root level web.config file.
            Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");

            //Modifying the AppKey from AppValue to AppValue1
            webConfigApp.AppSettings.Settings["AppKey"].Value = "AppValue1";



<appSettings>
    <add key="AppKey" value="AppValue"/>
  </appSettings>

2 Comments

I don't have problem reading web.config, I have problem reading app.config file...
I didn't try it working but it seems it would be solution I was looking for, however I used another approach eg. by creating custom config file with custom app settings section based on this social.msdn.microsoft.com/Forums/vstudio/en-US/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.