I don't understand why Mono isn't loading appSettings from an external file. I've seen others' posts detailing how they've achieved this in .NET and Mono. However, I'm only able to get it working in .NET.
I've tried the configSource and file attributes of appSettings. The only way the appSettings seem to load is to move them into the main config file.
Here is the code I currently have.
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings configSource="data\app.config" />
...
</configuration>
app.config
<appSettings>
<add key="AdminIcon" value="96d6f2e7e1f705ab5e59c84a6dc009b2.png" />
<add key="MailPort" value="25" />
<add key="GAEnable" value="False" />
...
</appSettings>
Reading Settings for Code-Behind
using System.Web.Configuration;
string adminIcon = WebConfigurationManager.AppSettings["AdminIcon"].Value;
My web server is running Ubuntu. In order to fix the issue outlined above, I needed to change the path delimiter from \ to /.
<!--<appSettings configSource="data\app.config" />-->
<appSettings configSource="data/app.config" />
This is all find and dandy, but this will now break .NET on Windows. This is likely a system dependent issue and not specifically bound to Mono or .NET.
Related
Trying to get simple <appSettings> for dev vs. prod.
My Web.config:
<appSettings>
<add key="hello" value="debug" />
</appSettings>
My Web.Release.config:
<appSettings>
<add key="hello" value="prod" />
</appSettings>
(both under <configuration>)
When I have it in Debug mode, and run my MVC site, I can do a simple return Content(WebConfigurationManager.AppSettings["hello"]); in my HomeController.Index and it returns dev. If I switch the mode to Release it still returns dev. I'd like to simulate prod mode without actually publishing to prod.
In the build-specific Web.config file, you have to tell it how to transform the base .config file. So to do what you ask, your Web.Release.config file should look like this:
<appSettings>
<add key="hello" value="prod" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
In the above code the SetAttributes transform will change the attributes of any element that matches the key attribute containing the value hello.
Starting from .NET 4.7.1 feature called Configuration builder is supported which gives developer ability to load configuration not only from Web.Release.Cong but basically from any source. Read more about .NET Framework 4.7.1 ASP.NET and Configuration features
I'm building a C# (WPF) application and I would like to use a simple configuration to define the file paths.
The purpose is for the user to be able to read and/or modify the configuration file easily and without having to learn any complicated syntax (or boilerplate), and to do it using a simple text editor.
I've been reading about the App.config file and from what I understand it is really complicated to modify by hand.
In the past in Windows and in Linux (even today) there were very simple Key=Value files that are exactly what I'm used to - however I see that C# doesn't have any builtin support for INI file reading/parsing.
Can an App.config file be modified easily by a user that isn't familiar with the syntax? If not is there any easy alternative?
For ease of editing, and to remove the complexity of the file as a whole, you can split the appSettings section out to a separate file, referenced from app.config...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!- stuff in here -->
</configSections>
<appSettings configSource="myCustomisableSettings.config" />
</Configuration>
The separate file should look like this.
<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
<add key="FirstPathKey" value="FirstPath" />
<add key="SecondPathKey" value="SecondPath" />
</appSettings>
For this situation, either using the app.config or web.config there is an appsetting's section that can contain key/value pairs for storing information such as file paths etc that are easily modifiable and readable:
<configuration>
<appSettings>
<add key="myFilePath" value="pathToFile" />
</appSettings>
....
</configuration>
you can add as many sections within the appSettings as you need
The App.config file is easily modified. The below is a standard App.config with a custom value.
<!-- Start Ignore here -->
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<!-- End ignore here, the below is what you want -->
<appSettings>
<add key="myCustomPath" value="C:\Path\To\Something"/> <!-- The user can edit this value -->
</appSettings>
</configuration>
As you can see all the user has to do is change the value under the appSettings node. It's that simple.
Then to access the value (in your code) all you have to do is call the ConfigurationManager:
var path = ConfigurationManager.AppSettings["myCustomPath"];
I have a class library application and am unable to configure a CONFIG file. My app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="test" value="value test"/>
</appSettings>
</configuration>
and the call in my application:
var test = ConfigurationManager.AppSettings["test"];
But when I run, always passes a null value.
Class libraries do not use a .config file. Only applications (app.config) and web site s(web.config) read the "config" file.
The class library can use the Configuration Manager to read a value, but they must running inside of an app/web site.
In ASP.Net rename 'app.config' to 'web.config' and place it in the root folder of web site.
You have to put the appSettings tag in the web.config of your website in order to use ConfigurationManager.AppSettings["test"] if you do not want to do that then make the setting xml file and read it in your library code.
Add this code to the web.confg
<configuration>
<appSettings>
<add key="test" value="value test"/>
</appSettings>
</configuration>
And this is get value
var test = WebConfigurationManager.AppSettings["test"];
If you are passing string you may use
in webconfig
<appSettings>
<add key="test" value="value test"/>
</appSettings>
In cs.
String update = Convert.ToString(ConfigurationManager.AppSettings["test "]);
thank u all!
I used my config from my web application and solved the problem. It was something simple that I was complicating.
I have more than one solution projects for an application with using one app.config for each solution.
Can i do a separate configuration file (other than app.config) for common setting like db name (connection string) ?
Because currently i put these setting in each and every app.config file.
Please try following.
Create one config called "common.config" like below which will be common to all solutions and let's say it's located # "E:/myconfig". Please keep all common setting in this config.
<appSettings>
<add key="connstring" value="conn string value" />
</appSettings>
Now link this common config in you specific solution config lets say web.config using file attribute
<configuration>
<appSettings file="E:\myconfig\Common.config">
<add key="key1" value="value" />
</appSettings>
</configuration>
Hope this will work for you.
I'm working on a ASP.NET project and I need to add some settings in appSettings section of my web-app.
Now these settings are growing up, so I'd like to organize them in different files. I've created other web.config files in different directories of my application, adding something like this:
<?xml version="1.0"?>
<configuration>
<system.web>
</system.web>
<appSettings>
<add key="settingKey" value="settingValue" />
</appSettings>
</configuration>
But when I try to access them via ConfigurationManager.AppSettings["settingKey"], I get null.
So, is it possible to split settings in different files? Is there another way to logically organize app settings values?
I know this is too old and probably doesn't even apply to .NET Core but for those coming from Google and using non-.NET Core json config files. Here's what I normally do...
I use configSources to take all config settings out of the web.config. This allows you to a specific config section to a different file by providing a relative location for example here's how you'd declare a configSource in a configuration section (in the root web.config file)...
<configuration>
<log4net configSource="Config\debug\log4net.config" />
<appSettings configSource="config\debug\settings.config" />
<connectionStrings configSource="config\debug\connections.config" />
...
</configuration>
You can name those files whatever you want, just make sure that the path specified and files exist in the solution. Here's what the settings.config file looks like...
<?xml version="1.0"?>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="foo" value="bar" />
</appSettings>
Now, the relative path is relative to the project root...
In picture above you can see that I have provided two different paths for different deployment environments, that's because obviously my conection strings and settings are different in production.
Then you can use configuration transformations so that the application can use the correct config files whether it is in debug or release mode...
This is what the Web.Debug.config file looks like...
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<log4net configSource="Config\debug\log4net.config" xdt:Transform="Replace" />
<appSettings configSource="config\debug\settings.config" xdt:Transform="Replace" />
<connectionStrings configSource="config\debug\connections.config" xdt:Transform="Replace" />
</configuration>
The release one is pretty much the same...replace the paths provided to the configSource attributes.And that's pretty much it.
There are other web.config elements that support configSource settings such as many of the system.serviceModel children element.
You will only be able to see the web.config settings within a directory if the currently executing path is within that directory.
So for example:
/MyDirectory/web.config
is only visible if you are loading a page like:
/Mydirectory/MyTestPage.aspx
You would not see the web.config settings here for example:
/OtherDirectory/MyTestPage.aspx
This post may help:
Creating a custom .config file in asp.net