ASP.NET solution with class library project

早过忘川 提交于 2019-12-04 19:30:31

问题


I have an solution in VS 2008 which contains two class library projects and an ASP.NET web site. The ASP.NET site references the class libraries and one of the libraries contains a LINQ To SQL item.

My question is with regards to the app.config in the class library which contains the connection string for the database. 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.

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.


回答1:


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. 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. You don't need to change anything in your code for the ConfigurationManager class to figure this out. It knows where to look automagically.

Hope that makes sense.




回答2:


You can edit the Web.config file in the final product. 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. 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).




回答3:


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.
    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.



回答4:


Copy the connectionString section from your library's app.config file to your web.config file. 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.




回答5:


If you want to have different settings for development vs production, use the Web Deployment Project. 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.



来源:https://stackoverflow.com/questions/435261/asp-net-solution-with-class-library-project

工具导航Map

JSON相关