获取占 applicationHost.config 的 AppSettings 的一致方法是什么?
因此,appSettings 可以在 app.config、web.config、machine、config 中设置...但是,当您在 IIS 7 下工作时,您还可以在 applicationHost.config 下应用 appSettings,并为您的站点进行特定的划分。那么,我知道应该使用什么 AppSettings 的一致方法是什么???
如果这是客户端配置文件,则 System.Configuration.ConfigurationManager 没问题。 System.Web.Configuration.WebConfigurationManager 如果这是一个网络应用程序......好吧,也许吧。 Microsoft.Web.Administration 如果我期望 applicationHosting.config 中的值 - 哎哟...即使如此,它们也不会按层次结构滚动,因此似乎您仍保留该过程。
是否有人有一致的方法来处理占 applicationHost.config 的 AppSettings?
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, 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上无法在
applicationHost.config
文件中指定
。这是因为applicationHost.config
仅定义特定于 IIS 的设置。您可以查看applicationHost.config
架构来确认这一点:如果您尝试编辑
applicationHost.config
并添加
> 部分或在
部分下,您将收到错误(IIS 可能无法启动,IIS MMC 控制台将显示配置错误)。如果您在 IIS 管理器中配置全局应用程序设置,则实际上会在与 IIS 中默认 .NET Framework 版本设置匹配的主
web.config
文件中进行配置。
特定于 .NET Framework,只能在以下位置进行配置:当然还有应用程序的
app.config
或web.config< /代码> 文件。
我的建议是将这些设置保留在您的应用程序本地,除非在极少数情况下您需要在全局范围内使用某些值。
更新:
现在我了解了您的问题 - 您有多个 IIS 站点,它们都指向同一个物理文件夹 - 有一种方法可以解决此问题。
您的数据库中可能有一个配置表,其中的主键为
HTTP_HOST
值。这映射到一个前缀,例如:在您的
web.config
中:在
Global.asax.cs
中应用程序的Application_Start
事件中初始化一个应用程序范围的值:该值将在整个应用程序中可用,但对于站点来说是唯一的。
当您需要读取特定于站点的设置时:
但是,如果您在
Application_Start
中访问数据库,那么实际上将所有这些特定于站点的设置存储在数据库中然后读取和将它们缓存在Application
中,而不是使用
。You can't actually specify
<appSettings>
in yourapplicationHost.config
file. This is becauseapplicationHost.config
defines settings specific to IIS only. You can review theapplicationHost.config
schema to confirm this: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:and of course your application's
app.config
orweb.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:In your
web.config
:In your application's
Application_Start
event inGlobal.asax.cs
initialise an application wide value: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:
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 inApplication
instead of using<appSettings />
.我使用了 Anti-Santa 的答案提供的信息来完成此任务,但我不想这样做解析 URL 并使用另一个数据库来查找信息。本地使用相同的代码库,在这种情况下我不会有这些部分。
只需使用
location
节点将appSettings
添加到 .NET Frameworkweb.config
即可。在
%SYSTEMROOT%\Microsoft.NET\Framework64\[framework version]\Config\web.config
中,添加以下内容,其中Site1
和Site2
我的两个应用程序是否指向同一个物理目录:物理文件夹中的
web.config
将覆盖此appSetting
,因此我还需要从该文件中将其删除。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 Frameworkweb.config
with alocation
node.In
%SYSTEMROOT%\Microsoft.NET\Framework64\[framework version]\Config\web.config
, add the following whereSite1
andSite2
are my two applications pointing to the same physical directory:The
web.config
in the physical folder will override thisappSetting
so I also needed to remove it from that file.