如何在 ASP.NET 中获取多个解决方案的多个重叠配置文件?

发布于 2024-10-28 08:10:20 字数 2477 浏览 1 评论 0原文

我对 ASP.net 很陌生,所以我只是想弄清楚这一点。

首先,我有多个单独的项目将托管在同一服务器上。

其次,它们必须能够共享某些设置(例如连接字符串、配置选项等)。

第三,这些共享设置必须可针对不同的部署(测试、生产等)进行配置。

例如,我有两个项目: 项目A 项目 B

我有一个名为“Setting1”的设置,两个项目都应该可以访问该设置。

“Setting1”的值应来自以下文件之一: “测试配置.xml” “PROD-config.xml” (将有两个以上的配置文件,但我现在会保持简单)

并且我只想有一个位置来更改单个文本文件,该文件确定将使用哪个 *-config.xml 文件:

“WhichSettingsFile.xml”

以下是文件结构的示例:

\WhichSettingsFile.xml \TEST-config.xml \PROD-config.xml \Project1\Project1.csproj \Project1\web.config \Project1...(更多文件) \Project2\Project2.csproj \Project2\web.config \Project2... (更多文件)

所以目前,每个项目都有一个像这样的“web.config”:

<appSettings file="..\WhichSettingsFile.xml">
    ...
</appSettings>

然后我有一个像这样的文件“WhichSettingsFile.xml”:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="SettingsFilenamePrefix" value="PROD" />
</appSettings>

我有这样的代码:

public static string GetSetting(name) {
    string filename = System.Web.Configuration.WebConfigurationManager.AppSettings["SettingsFilenamePrefix"] + "-config.xml";
    // Open filename, parse as XML, get the values I need from it.
}

这意味着我可以托管两个项目服务器 A 上的项目,将 WhichSettingsFile.xml 设置为包含“PROD”,所有设置将从“PROD-config.xml”中读取。我可以对测试服务器做同样的事情。所有这些价值观在不同的项目之间共享。

但是,我需要能够在文件系统上编辑这些文件,而不是将它们嵌入到 dll/程序集中。当我从 Visual Studio 运行项目时,内置的 asp.net 托管服务器会将文件部署到以下位置:

C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\lskdjflskj\slkdjfsld\klsjdfwbe

但不会有“WhichSettingsFile.xml”文件,因为它嵌入在程序集中。此外,“PROD-config.xml”和“TEST-config.xml”文件将不会被包含在内,因为它们实际上并不是“代码”的一部分,并且不会被复制。

理想情况下,我希望将其放在部署文件夹中:

C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\Project1\(all the compiled files here)
C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\Project2\(all the compiled files here)
C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\WhichSettingsFile.xml
C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\PROD-config.xml
C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\TEST-config.xml

但我不确定如何执行此操作。

所以我的问题是: -“WhichSettingsFile.xml”嵌入到程序集中。我希望这是文件系统上的纯文本文件,由应用程序在运行时引用。我意识到我可能需要避免使用这个。有更好的办法吗? -我想要多个配置文件,也作为文件系统上的纯文本文件。

我知道我所拥有的不起作用,而且可能很复杂。但有更好的方法吗?谢谢!!

I'm very new to ASP.net, so I'm just figuring this out.

First off, I have multiple separate projects that will be hosted on the same server.

Second, they must be able to share certain settings (like connection string, configuration options, etc).

Third, those shared settings must be configurable for different deployments (test, prod, etc).

For example, I have two projects:
Project A
Project B

I have a setting called "Setting1" which should be accessible to both projects.

The value for "Setting1" should come from one of these files:
"TEST-config.xml"
"PROD-config.xml"
(There will be more than two config files, but I'll keep it simple for now)

And I want to have only a single place to change a single text file, which determines which of the *-config.xml files will be used:

"WhichSettingsFile.xml"

Here's an example of the file structure:

\WhichSettingsFile.xml
\TEST-config.xml
\PROD-config.xml
\Project1\Project1.csproj
\Project1\web.config
\Project1... (more files)
\Project2\Project2.csproj
\Project2\web.config
\Project2... (more files)

So currently, each project has a "web.config" like this:

<appSettings file="..\WhichSettingsFile.xml">
    ...
</appSettings>

Then I have a file "WhichSettingsFile.xml" like this:

<?xml version="1.0" encoding="utf-8" ?>
<appSettings>
    <add key="SettingsFilenamePrefix" value="PROD" />
</appSettings>

And I have code like this:

public static string GetSetting(name) {
    string filename = System.Web.Configuration.WebConfigurationManager.AppSettings["SettingsFilenamePrefix"] + "-config.xml";
    // Open filename, parse as XML, get the values I need from it.
}

This means I can host both projects on Server A, set the WhichSettingsFile.xml to contain "PROD", and all settings will be read from "PROD-config.xml". I can do the same for the test server. And all those values are shared among the separate projects.

However, I need to be able to edit these files on the filesystem, and not have them embedded within the dlls / assemblies. When I run the project(s) from visual studio, the built-in asp.net hosting server will deploy the files to something like:

C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\lskdjflskj\slkdjfsld\klsjdfwbe

But there will be no "WhichSettingsFile.xml" file since it's embedded in the assembly. Furthermore, the "PROD-config.xml" and "TEST-config.xml" files will not be included since they're not really part of the "code", and are not copied.

Ideally, I want to have this in the deployment folder:

C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\Project1\(all the compiled files here)
C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\Project2\(all the compiled files here)
C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\WhichSettingsFile.xml
C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\PROD-config.xml
C:\Users\[username]\AppData\Local\Temp\6\Temporary ASP.NET Files\abc123\TEST-config.xml

But I'm not sure how to do this.

So my problems:
-The "WhichSettingsFile.xml" is embedded into the assembly. I want this to be a plain text file on the filesystem that is referenced at runtime by the application. I realize I might need to avoid using for this. Is there a better way?
-I want to have multiple config files, also as plain text files on the filesystem.

I know what I've got is not working, and probably convoluted. But is there a better way to do this? Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

我们只是彼此的过ke 2024-11-04 08:10:20

每个目录可以有一个单独的 web.config。
根目录的web.config会影响所有子文件夹。但可以使用本地 web.config 文件覆盖子文件夹中的值。

您可以使用 添加新值,您可以使用 删除全局 web.config 中设置的现有值code>,您可以使用 清除某个部分中的所有值。

http://weblogs.asp.net/mnolton/archive/2005 /01/10/349986.aspx

如果单独的目录不适合您的体系结构,您可以将配置存储在数据库中,这在大多数情况下都是理想的,因为它提供缓存、更改值的能力/重新启动应用程序、在多个 Web 服务器之间共享设置、更轻松的部署(因为更改设置时不必部署文件)等等。

you can have a separate web.config per directory.
the root directory web.config will affect all sub folders. but values can be overwritten in sub folders with local web.config files.

you can add a new values with <add ... />, you can remove exist value set in global web.config with <remove ... />, you can clear all values in a section using <clear />.

http://weblogs.asp.net/mnolton/archive/2005/01/10/349986.aspx

If having separate directories doesn't fit with your architecture, you can store your configuration in a database, which is ideal in most scenarios as it offers caching, ability to change values w/out restarting application, sharing settings among multiple web servers, easier deployments (since you don't have to deploy files when changing settings) and so on.

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