获取占 applicationHost.config 的 AppSettings 的一致方法是什么?

发布于 2024-10-07 19:57:51 字数 490 浏览 0 评论 0原文

因此,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-10-14 19:57:51

您实际上无法在 applicationHost.config 文件中指定 。这是因为 applicationHost.config 仅定义特定于 IIS 的设置。您可以查看 applicationHost.config 架构来确认这一点:

%systemroot%\System32\inetsrv\config\schema\IIS_schema.xml

如果您尝试编辑 applicationHost.config 并添加 > 部分或在 部分下,您将收到错误(IIS 可能无法启动,IIS MMC 控制台将显示配置错误)。

如果您在 IIS 管理器中配置全局应用程序设置,则实际上会在与 IIS 中默认 .NET Framework 版本设置匹配的主 web.config 文件中进行配置。

特定于 .NET Framework,只能在以下位置进行配置:

%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

当然还有应用程序的 app.configweb.config< /代码> 文件。

我的建议是将这些设置保留在您的应用程序本地,除非在极少数情况下您需要在全局范围内使用某些值。

更新:

现在我了解了您的问题 - 您有多个 IIS 站点,它们都指向同一个物理文件夹 - 有一种方法可以解决此问题。

您的数据库中可能有一个配置表,其中的主键为 HTTP_HOST 值。这映射到一个前缀,例如:

Host               SitePrefix
====               ======
domain1.com        D001
domain2.com        D002

在您的 web.config 中:

Global.asax.cs 中应用程序的 Application_Start 事件中初始化一个应用程序范围的值:

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);
}

该值将在整个应用程序中可用,但对于站点来说是唯一的。

当您需要读取特定于站点的设置时:

string siteSetting = ConfigurationManager.AppSettings[
    HttpContext.Current.Application["SitePrefix"] + "_Setting1"
];

但是,如果您在 Application_Start 中访问数据库,那么实际上将所有这些特定于站点的设置存储在数据库中然后读取和将它们缓存在 Application 中,而不是使用

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 />.

匿名。 2024-10-14 19:57:51

我使用了 Anti-Santa 的答案提供的信息来完成此任务,但我不想这样做解析 URL 并使用另一个数据库来查找信息。本地使用相同的代码库,在这种情况下我不会有这些部分。

只需使用 location 节点将 appSettings 添加到 .NET Framework web.config 即可。

%SYSTEMROOT%\Microsoft.NET\Framework64\[framework version]\Config\web.config 中,添加以下内容,其中 Site1Site2我的两个应用程序是否指向同一个物理目录:

<?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> 

物理文件夹中的 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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文