MVC 4 dedicated configuration file - c#

I have to add some constants to MVC 4 project.
Adding them to in web.config will work:
<appSettings>
<add key="MyConst1" value="123"/>
<add key="MyConst2" value="321"/>
<add key="MyConst3" value="234"/>
</appSettings>
Is there a way to create separate config file for this constants?

I guess you want to separate your appSettings from web.config, you can store them in a separate file and then specify that in your web.config under appSettings's configSource like:
<appSettings configSource="MySettings.config" />
and then you can have your settings in MySettings.config as:
<?xml version="1.0" encoding="UTF-8"?>
<appSettings>
<add key="MyConst1" value="123"/>
<add key="MyConst2" value="321"/>
<add key="MyConst3" value="234"/>
</appSettings>
You may see: Using configSource to split configuration files

If you put your code snippet in a file called appSettings.config, you can simply reference that file in your web config:
<appSettings configSource="appSettings.config" />

Related

Can't read connection string from web.config

I have the following in my web.config located at the root of my project:
<configuration>
<connectionStrings>
<clear />
<add name="Default" providerName="System.Data.SqlClient" connectionString="Server=tcp:whoops;Encrypt=True;TrustServerCertificate=False;Connection Timeout=3000;" />
</connectionStrings>
<appSettings>
<add key="ConnectionString" value="test"/>
</appSettings>
....
I read from Startup.cs (this is an asp.net core web app):
string connection = ConfigurationManager.ConnectionStrings["Default"].ConnectionString;
However when I break on this, ConfigurationManager.ConnectionStrings and ConfigurationManager.AppSettings are empty (well, the first has some default connection string that is not the one in web.config).
What's going on here?
You will have to migrate the config to the new file appsettings.json
https://learn.microsoft.com/en-us/aspnet/core/migration/configuration?view=aspnetcore-2.1
Not saying this is how you should do it, but you can do the following...
In ASP.Net Core 2.2, you should be able to add an XML configuration to IConfigurationBuilder using
configBuilder.AddXmlFile("app.config");
Contents is pretty much the same as above...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings configSource="connectionStrings.config" />
<appSettings>
<add key="Test" value="TestValue" />
</appSettings>
</configuration>
You should then be able to access AppSettings/ConnectionStrings using...
ConfigurationManager.ConnectionStrings
ConfigurationManager.AppSettings.AllKeys
{string[1]}
[0]: "Test"
ConfigurationManager.AppSettings.Get("Test")
"TestValue"

Modify appSettings in the custom config file which is referenced via 'file' attribute

In appSettings section of Web.config a file attribute is used referencing a custom config file. The goal is to have possibility to modify some app-settings in the custom config without causing the application to be restarted.
Web.config
<appSettings file="CustomAppSettings.config">
<add key="key1" value="val2" />
</appSettings>
CustomAppSettings.config
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="customKey1" value="custVal2"/>
</appSettings>
The following code does not work. It saves the value to Web.config but expected is to save it to the CustomAppSettings.config because so it will not restart the application (Source).
var configuration = WebConfigurationManager.OpenWebConfiguration("~/");
configuration.AppSettings.Settings[key].Value = value.ToString();
configuration.Save();
This does not work as well.
var configuration = WebConfigurationManager.OpenWebConfiguration("~/CustomAppSettings.config");
What am I doing wrong? Could someone point me to the right direction?
use configSource instead of file.
<appSettings configSource="CustomAppSettings.config" />
use ConfigurationSaveMode.Minimal on saving.
var configuration = WebConfigurationManager.OpenWebConfiguration("~/");
configuration.AppSettings.Settings[key].Value = value.ToString();
configuration.Save(ConfigurationSaveMode.Minimal);

Reading settings from app.config in .net 4.5

I've added a reference to System.Configuration. I've created App1.config in my project and populated it with the following code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ResistanceA" value="0.04"/>
<add key="ResistanceB" value="0.04"/>
<add key="ResistanceC" value="0.01"/>
<add key="TempBattLow" value="40"/>
<add key="TempBattHigh" value="45"/>
<add key="TempLoad" value="40"/>
</appSettings>
</configuration>
Then I try to read the values using the following code,
using System.Configuration;
string str = ConfigurationManager.AppSettings.Get("ResistanceA");
However I do not get the data. Any idea what I am doing wrong? Thanks.
Make sure the (app name here).config file actually appears in the same folder as your (app name here).exe file. Being that you called it App1.config, I'm guessing that you have more than one.
Visual Studio renames App.Config to the actual (app name here).config file during a build, not App1.config.

Get winform Webbrowsercontrol Source from app config file

Can someone please show me how to get winforms Webbrowsercontrol source from appconfig file ?.
I tried with following but it's not working
webBrowser2.Navigate = ConfigurationManager.AppSettings["dateandtime"];
and this is my how app config file look like
<?xml version="1.0"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="serverip" value="127.0.0.1" />
<add key="dbport" value="3306" />
<add key="defdatabase" value="waq115" />
<add key="dateandtime" value="http://free.timeanddate.com/clock/i4daxch9/n77/fs18/fcfff/tc212426/pc212426" />
</appSettings>
<startup><supportedRuntime version="v2.0.50727"/></startup>
</configuration>
Navigate() is a method not a variable, So the following line of code will not compile,
webBrowser2.Navigate = ConfigurationManager.AppSettings["dateandtime"];
You have to read the URL from the application configuration file and pass it as an argument to Navigate("URL") method as follows.
webBrowser1.Navigate(ConfigurationManager.AppSettings["dateandtime"]);

Best practice to include log4Net external config file in ASP.NET

I have seen at least two ways to include an external log4net config file in an ASP.NET web application:
Having the following attribute in your AssemblyInfo.cs file:
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log.config", Watch = true)]
Calling the XmlConfigurator in the Global.asax.cs:
protected void Application_Start()
{
XmlConfigurator.Configure(new FileInfo("Log.config"));
}
What would be the best practice to do it?
At startup, call:
XmlConfigurator.Configure();
In your Web.config, specify log4net.Config in appSettings:
<add key="log4net.Config" value="Log.config" />
This special setting allows you to change the log configuration without having to recompile. Especially helpful for moving between multiple environments.
Example
Consider the following project file structure:
\config\log4net\debug.config
\config\log4net\staging.config
\config\log4net\release.config
\config\appSettings\debug.config
\config\appSettings\staging.config
\config\appSettings\release.config
Application and logging configurations are distinguished for each environment. References to the logging configurations are maintained in the application settings.
\config\appSettings\debug.config:
<appSettings>
<add key="log4net.Config" value="config\log4net\debug.config" />
...
</appSettings>
\config\appSettings\staging.config:
<appSettings>
<add key="log4net.Config" value="config\log4net\staging.config" />
...
</appSettings>
\config\appSettings\release.config:
<appSettings>
<add key="log4net.Config" value="config\log4net\release.config" />
...
</appSettings>
Changing environments is a simple matter of updating the appSettings file in Web.config.
<appSettings file="config\appSettings\staging.config">
...
</appSettings>
I was dissatisfied with the "magic" configuration approach, because I wanted to specify my configuration in a path with an environment variable (%PUBLIC%\MyApp\MySettings.config).
So instead, I have this in my app.config:
<add key="MyLog4NetConfigFile" value="%PUBLIC%\MyApp\MySettings.config"/>
And do this to set my log4net configuration:
var configFile = ConfigurationManager.AppSettings.Get("MyLog4NetConfigFile");
if( !string.IsNullOrEmpty(configFile))
{
configFile = Environment.ExpandEnvironmentVariables(configFile);
XmlConfigurator.Configure(new FileInfo(configFile));
}

Categories