[英]ASP.NET solution with class library project
I have an solution in VS 2008 which contains two class library projects and an ASP.NET web site. 我在VS 2008中有一个解决方案,它包含两个类库项目和一个ASP.NET网站。 The ASP.NET site references the class libraries and one of the libraries contains a LINQ To SQL item. ASP.NET站点引用类库,其中一个库包含LINQ To SQL项。
My question is with regards to the app.config in the class library which contains the connection string for the database. 我的问题是关于类库中的app.config,它包含数据库的连接字符串。 When I build the project, this app.config isn't within the build directory and this means I can't dynamically change the connection string for the deployed project. 当我构建项目时,此app.config不在构建目录中,这意味着我无法动态更改已部署项目的连接字符串。
What am I doing wrong here, how can I have these settings deployed too so I can make changes to the connection string? 我在这里做错了什么,如何部署这些设置,以便我可以更改连接字符串?
Thanks in advance, 提前致谢,
Martin. 马丁。
This caused me a bit of confusion at first as well. 这也引起了我一点困惑。
You might think that the class library uses the app.config file that's contained in it's own project but it doesn't. 您可能认为类库使用的是包含在其自己的项目中的app.config文件,但它没有。 It uses the config file of the project that is referencing it. 它使用引用它的项目的配置文件。
So what you need to do is look for the <appSettings/>
tag inside the web.config file of your ASP.Net project and change it to <appSettings></appSettings>
And add the <add ... />
tags that are contained in the app.config file of the library project. 因此,您需要做的是在ASP.Net项目的web.config文件中查找<appSettings/>
标记并将其更改为<appSettings></appSettings>
并添加<add ... />
标记包含在库项目的app.config文件中。 You don't need to change anything in your code for the ConfigurationManager class to figure this out. 您不需要在代码中更改ConfigurationManager类的任何内容来解决这个问题。 It knows where to look automagically. 它知道自动查找的位置。
Hope that makes sense. 希望有道理。
To overcome the problem of connection string, here is the trick 为了克服连接字符串的问题,这是诀窍
You can edit the Web.config
file in the final product. 您可以在最终产品中编辑Web.config
文件。 Configuration APIs normally will get configuration data from the primary configuration file of the application which, in case of ASP.NET apps is the Web.config
and for client applications is Myfile.exe.config
. 配置API通常会从应用程序的主配置文件中获取配置数据,如果ASP.NET应用程序是Web.config
,则客户端应用程序是Myfile.exe.config
。 It's important to know that class libraries in the project usually will not have their separate configuration file like MyClassLib.dll.config
(unless you manually refer to the specific file). 重要的是要知道项目中的类库通常不会有像MyClassLib.dll.config
这样的单独配置文件(除非您手动引用特定文件)。
Copy the connectionString section from your library's app.config file to your web.config file. 将connectionString部分从库的app.config文件复制到web.config文件。 Change the actual connection string from your development to your production server as necessary. 根据需要将实际连接字符串从开发更改为生产服务器。 The ConfigurationManager class that LINQ2SQL uses to obtain the connection string will look in the web.config file for the appropriately named connection string and use it if it exists. LINQ2SQL用于获取连接字符串的ConfigurationManager类将在web.config文件中查找适当命名的连接字符串,如果存在则使用它。
If you want to have different settings for development vs production, use the Web Deployment Project. 如果要为开发与生产设置不同的设置,请使用Web部署项目。 Download here . 在这里下载。 From Microsoft's description: 来自微软的描述:
Visual Studio 2008 Web Deployment Projects provide additional functionality to build and deploy Web sites and Web applications in Visual Studio 2008. This add-in provides a comprehensive UI to manage build configurations, merging, and using pre-build and post-build tasks with MSBuild. Visual Studio 2008 Web部署项目提供了在Visual Studio 2008中构建和部署Web站点和Web应用程序的附加功能。此加载项提供了一个全面的UI,用于管理构建配置,合并以及使用MSBuild进行预构建和后构建任务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.