简体   繁体   English

是否可以在解决方案中的多个项目之间共享 web.config?(ASP.NET)

[英]Is it possible to share the web.config across multiple projects in a solution?(ASP.NET)

I have a solution composed of a web application and multiple projects, is it possible to share the configuration in the web application across all the solution?我有一个由 Web 应用程序和多个项目组成的解决方案,是否可以在所有解决方案中共享 Web 应用程序中的配置?

Thanks谢谢

I looks like it is possible, you can try following:我看起来是可能的,您可以尝试以下操作:

You can copy your web.config file to the root folder of your solution.您可以将web.config文件复制到解决方案的根文件夹。 Then create solution folder (which does not belong to any of your projects) and add your file there (via Add Existing Item popup menu item).然后创建解决方案文件夹(不属于您的任何项目)并在那里添加您的文件(通过Add Existing Item弹出菜单项)。 Delete web configs from your web sites.从您的网站中删除网络配置。 After that you can select Add Existing Item from popup menu on your both web projects, select your web.config file from the solution root and click Add As Link .之后,您可以从两个 Web 项目的弹出菜单中选择Add Existing Item ,从解决方案根目录中选择您的 web.config 文件,然后单击Add As Link After that you will have links to your web config files instead of real ones in your solution.之后,您将拥有指向 Web 配置文件的链接,而不是解决方案中的真实文件。

替代文字

On the other hand I'm not sure if sharing of the web.config files among different projects is a good idea, because usually web.config has some specific condifuration for each separate application.另一方面,我不确定在不同项目之间共享 web.config 文件是否是一个好主意,因为通常 web.config 对每个单独的应用程序都有一些特定的配置。

No, it is not possible.不,这是不可能的。 A web.config file need to be placed at the root folder of each web site. web.config 文件需要放置在每个网站的根文件夹中。 You could have a common web.config file copied to the root of each site as a build step of the project so that at runtime ASP.NET can find it.您可以将一个公共 web.config 文件复制到每个站点的根目录作为项目的构建步骤,以便在运行时 ASP.NET 可以找到它。

Web.config files contain cascading overrides of a higher web.config file settings. Web.config 文件包含更高的 web.config 文件设置的级联覆盖。 So if you would place a web.config in the root of IIS or adjust settings in the machine.config or standard web.config of the .NET framework (C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\CONFIG for example).因此,如果您将 web.config 放在 IIS 的根目录中或调整 .NET 框架的 machine.config 或标准 web.config 中的设置(C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\CONFIG for例)。

The best example's probably the use of the LocalSqlServer connection string.最好的例子可能是使用 LocalSqlServer 连接字符串。 It's defined in the machine.config as它在 machine.config 中定义为

<connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;  
    Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;
    User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

If you would use the Login control for example and would debug and request the connectionstrings then this one will be included as well.例如,如果您将使用 Login 控件并调试和请求连接字符串,那么也将包含此控件。

So if you need some settings common to multiple applications place them in a web.config file which is high enough in the hierarchy, ultimately in the machine.config.因此,如果您需要一些对多个应用程序通用的设置,请将它们放在层次结构中足够高的 web.config 文件中,最终放在 machine.config 中。

There's another alternative, and that is to put in a Pre-build command to copy the web.config from one folder to your destination folder.还有另一种选择,那就是放入一个预构建命令,将 web.config 从一个文件夹复制到您的目标文件夹。 Let's say you had 3 projects.假设您有 3 个项目。 Project A has the "master" web.config.项目 A 具有“主”web.config。

In Project B and Project C, set up a pre-build to copy the web.config to the root of each project file, overwriting the web.config that exists in those folders.在项目 B 和项目 C 中,设置预构建以将 web.config 复制到每个项目文件的根目录,覆盖这些文件夹中存在的 web.config。

The main problem with this is that you'll probably have specific bindings in each web.config that gets overwritten.这样做的主要问题是您可能在每个 web.config 中都有特定的绑定被覆盖。

If your goal is to copy appSettings and possibly connectionStrings, you could do the same pre-build as above, but keep the appsettings and connectionstrings in a separate file from the web.config, then specifically open the settings file and read the content yourself using the WebConfigurationManager.OpenWebConfiguration method.如果您的目标是复制 appSettings 和可能的 connectionStrings,您可以执行与上述相同的预构建,但将 appsettings 和 connectionstrings 保存在与 web.config 不同的文件中,然后专门打开设置文件并使用自己阅读内容WebConfigurationManager.OpenWebConfiguration 方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 加密打包的ASP.NET解决方案中的Web.Config值 - Encrypt Web.Config value in packaged ASP.NET solution 如何设置Jenkins作业来处理具有多个配置的ASP.Net解决方案(仅在Web.config中有所不同) - How to setup Jenkins job to handle ASP.Net Solution with multiple configurations (differing only in Web.config) 如何在 Visual Studio 2015 中的一个解决方案中跨多个 ASP.NET CORE 项目共享 appsettings.json 等配置文件? - How can I share config file like appsettings.json across multiple ASP.NET CORE projects in one solution in Visual Studio 2015? ASP.NET Web应用程序中的多个Web.Config文件 - Multiple Web.Config files in ASP.NET web application 是否可以在Asp.net Web.config中添加多个内容安全策略指令? - Is it possible to add multiple Content Security Policy directive in Asp.net Web.config? asp.net web.config appsettings 多个值 - asp.net web.config appsettings multiple values web.config中的多个元素-asp.net - multiple elements in web.config - asp.net ASP.NET 多个 web.config 文件 - ASP.NET Multiple web.config file ASP.NET(web.config + sql)中的多个成员资格提供程序 - Multiple membership providers in ASP.NET (web.config + sql) asp.net管理多个web.config文件 - asp.net managing multiple web.config files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM