So, appSettings can be set in app.config, web.config, machine,config... But, when you work under IIS 7 you can also have appSettings being applied under applicationHost.config with a specific carve out for your site. So, what's a consistent way for me to know what AppSettings I should be using???
System.Configuration.ConfigurationManager is fine if this is a client profile. System.Web.Configuration.WebConfigurationManager if this is a web-app ... well,开发者_StackOverflow中文版 maybe. Microsoft.Web.Administration if I expect values in applicationHosting.config - ouch ... and even then they don't roll-up hierarchically so it seems that you're left holding that process.
Does anyone have a consistent approach to processing AppSettings that accounts for applicationHost.config?
You can't actually specify <appSettings>
in your applicationHost.config
file. This is because applicationHost.config
defines settings specific to IIS only. You can review the applicationHost.config
schema to confirm this:
%systemroot%\System32\inetsrv\config\schema\IIS_schema.xml
If you try to edit applicationHost.config
and add an <appSetting>
section to a site or under a <location path="...">
section you will get an error (IIS may not start and IIS MMC console will display a configuration error).
If you configure a global application setting in IIS manager this actually gets configured in the master web.config
file that matches the default .NET Framework version setting in IIS.
<appSettings/>
is specific to the .NET Framework and can only be configured in:
%SYSTEMROOT%\Microsoft.NET\Framework\[framework version]\CONFIG\machine.config %SYSTEMROOT%\Microsoft.NET\Framework\[framework version]\CONFIG\web.config %SYSTEMROOT%\Microsoft.NET\Framework64\[framework version]\CONFIG\machine.config %SYSTEMROOT%\Microsoft.NET\Framework64\[framework version]\CONFIG\web.config
and of course your application's app.config
or web.config
files.
My advice would be to keep these settings local to your application unless there is the rare occasion where you need some value to be available globally.
Update:
Now that I understand your problem - you have multiple IIS sites which all point to the same physical folder - there is a way to approach this.
You could have a configuration table in your database that has a primary key of whatever the HTTP_HOST
value is. This maps to a prefix, for example:
Host SitePrefix ==== ====== domain1.com D001 domain2.com D002
In your web.config
:
In your application's Application_Start
event in Global.asax.cs
initialise an application wide value:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
string httpHost = HttpContext.Current.Request.ServerVariables["HTTP_HOST"];
Application["SitePrefix"] = GetSiteKeyFromDb(httpHost);
}
This value will be available across the whole application but unique to the site.
When you need to read a setting that is specific to the site:
string siteSetting = ConfigurationManager.AppSettings[
HttpContext.Current.Application["SitePrefix"] + "_Setting1"
];
But if you're hitting the database in Application_Start
then it may actually be expedient to store all these site specific settings in the database then read and cache them in Application
instead of using <appSettings />
.
I used the information provided by Anti-Santa's answer to accomplish this but I didn't want to parse the URL and have another database to look up information. The same codebase is used on-premise and I wouldn't have those pieces in that scenario.
Just add the appSettings
to the .NET Framework web.config
with a location
node.
In %SYSTEMROOT%\Microsoft.NET\Framework64\[framework version]\Config\web.config
, add the following where Site1
and Site2
are my two applications pointing to the same physical directory:
<?xml version="1.0" encoding="utf-8"?>
<!-- the root web configuration file -->
<configuration>
<!-- App settings for the different applications -->
<location path="Default Web Site/Site1">
<appSettings>
<add key="ConnectionString" value="the cnn str"/>
</appSettings>
</location>
<location path="Default Web Site/Site2">
<appSettings>
<add key="ConnectionString" value="the cnn str"/>
</appSettings>
</location>
...
</configuration>
The web.config
in the physical folder will override this appSetting
so I also needed to remove it from that file.
精彩评论