在运行时为 ASP.NET 加载不同的 web.config 文件 [英] Loading a different web.config file at runtime fo ASP.NET

查看:23
本文介绍了在运行时为 ASP.NET 加载不同的 web.config 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web 应用程序,我想将其用于多个业务领域.我不想在 Web 服务器上有多个 Web 应用程序代码实例.

I have a web application which I want to use for multiple business areas. I do not want to have multiple instance of the web app code on the web server.

举个例子:我有//MyWebSite/Virtual_Directory_1 指的是 c:\Inetpub\wwwroot\MyWebApp 指向 Database_A我想要的是设置另一个虚拟目录如下://MyWebSite/Virtual_Directory_2 指的是指向 Database_B 的 c:\Inetpub\wwwroot\MyWebApp.

So as an example: I have //MyWebSite/Virtual_Directory_1 refers to c:\Inetpub\wwwroot\MyWebApp pointing to Database_A What I want is to setup another Virtual Directory as follows: //MyWebSite/Virtual_Directory_2 refers to c:\Inetpub\wwwroot\MyWebApp pointing to Database_B.

我想这样做是通过单独的 web.config 文件,但我不知道如何实际做到这一点.

The I was thinking of doing this was by having separate web.config files, but I am not sure how to actually do this.

任何帮助将不胜感激.

谢谢段

推荐答案

我想这样做的一种方法是拥有一个共享的 web.config 文件,然后在该配置文件中有多个与虚拟目录名称匹配的连接字符串.

One way of doing it I guess would be to have a shared web.config file and then have multiple connection strings in that config file that match the virtual directory name.

<connectionStrings>
    <add name="VirtualDir1" connectionString="data source=BLAHBLAH" />
    <add name="VirtualDir2" connectionString="data source=BLAHBLAH" />
</connectionStrings>

获取连接字符串时只需引用如下:

When getting the connection string simply reference like:

ConfigurationManager.ConnectionStrings(Request.ApplicationPath).ConnectionString

虽然说实话,我认为花一点时间在构建项目上,并且拥有多个带有自己的配置文件的代码副本,从长远来看会省去你的痛苦.

Although to be honest I think a little time spent with build projects and having multiple copies of the code with their own config files will save you pain in the long run.

这篇关于在运行时为 ASP.NET 加载不同的 web.config 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆