开发者

How do I get multiple overlapping config files for multiple solutions in ASP.NET?

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-con开发者_开发问答fig.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!!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜