How can I make a dynamic route in app.config on C#. Because this project will run on multiple machines.
<appSettings>
<add key="RutaEvidencias"
value="D://Users/WA5699CR/OneDrive - GrupoMonge/Desktop/Evidencias/"/>
</appSettings>
But I need to do it dynamic
Related
I have a solution with a bunch of .net framework applications that are all configured to use Seq for logging. Currently I have the same settings duplicated in every app.config file:
<appSettings>
<add key="serilog:using:Seq" value="Serilog.Sinks.Seq" />
<add key="serilog:write-to:Seq.serverUrl" value="http://localhost:5341" />
</appSettings>
If I would like to be able to configure the Seq URL for all the applications from a single file, not only when running in Visual Studio but also when the system has been deployed.
How can this be done?
I have looked into creating a common settings file and include it in all applications using <appSettings file="CommonSettings.config">, but from what I understand this actually creates a copy of the file for each application.
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
How can I set a different value when I publish my MVC5 do production server?
Example:
In dev I have
<appSettings>
<add key="XLSFile" value="C:\\temp\\file.xls" />
</appSettings>
And when I publish the project I want to set a different path:
<appSettings>
<add key="XLSFile" value="C:\\projectname\\file.xls" />
</appSettings>
You'll want to use a config file transformation. Essentially you will override the config file for your release build.
For example:
<add key="XLSFile" value="C:\\projectname\\file.xls" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
You can use two config files one with name web.Debug.config and second web.Release.config. First one for development and second for production server when published and now specify different value of key in both files as required
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.