简体   繁体   English

带有类库项目的ASP.NET解决方案

[英]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 为了克服连接字符串的问题,这是诀窍

  1. Inside ur class library declare module that has got two properties, one is a setter and the other is a getter, and make them public. 在ur类库中声明了有两个属性的模块,一个是setter,另一个是getter,并将它们公之于众。
    1. Inside ur website project, go to the global file, and under both session start and application start call the setter property that u declared previously, and assign it the connection string that is located in ur web.config, now the connection string will be available in the website general scope and the value exists as long as ur session credential not expired. 在你的ur网站项目中,转到全局文件,在session start和application start下调用你之前声明的setter属性,并为它分配ur web.config中的连接字符串,现在连接字符串将可用在网站的一般范围内,只要您的会话凭证未过期,该值就存在。

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.

相关问题 是否可以在另一个解决方案中从 ASP.NET Webforms 项目中引用 ASP.NET Core 2.0 类库 dll? - Possible to reference an ASP.NET Core 2.0 class library dll from an ASP.NET Webforms project in another solution? 将带有类库的ASP.NET Core解决方案部署到Azure - Deploying ASP.NET Core solution with class library to Azure 如何在ASP.NET中将一个类库项目插入另一个类库项目的另一个类中? - How to impelement one class of class library project into another class of another class library project in asp.net? 如何以编程方式将文件添加到类库项目ASP.NET - How to programatically add a file to a Class Library Project ASP.NET 为什么要创建ASP.NET 5类库项目? - Why create an ASP.NET 5 Class Library project? 访问WCF的ASP.Net类库项目 - ASP.Net Class Library project accessing WCF ASP.NET MVC 和 Web API 共享类库项目 - ASP.NET MVC and Web API sharing Class Library Project 在ASP.NET 5(Core)中添加对类库项目的引用的问题 - Issue adding reference to class library project in ASP.NET 5 (Core) ASP.net 5.0 vnext类库项目的模拟框架 - Mocking framework for ASP.net 5.0 vnext class library project Asp.net项目无法识别我的类库中的文件夹之一 - Asp.net project is not recognising one of the folders in my class library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM