简体   繁体   English

在另一个项目中使用web.config?

[英]Use web.config in another project?

I'm working on a visual studio solution which was developed by others long time ago. 我正在研究由其他人很久以前开发的Visual Studio解决方案。 It is for asp.net 2. Now, need to add some new features. 它是为asp.net2。现在,需要添加一些新功能。

In the solution, it has several projects, including myProject.Data, myProject.Business, ..., myProject.Website . 在解决方案中,它具有多个项目,包括myProject.Data,myProject.Business,...,myProject.Website。 Over time, different developers made the changes. 随着时间的流逝,不同的开发人员进行了更改。

In the myProject.Data, it defines a SQL server connection string as a constant, and use it do some SQL work. 在myProject.Data中,它将SQL Server连接字符串定义为常量,并使用它来执行一些SQL工作。 This is used in myProject.Website. 这在myProject.Website中使用。 In the myProject.Website, it also reads some files from a network drive, and do some processing too. 在myProject.Website中,它还从网络驱动器中读取一些文件,并进行一些处理。

Now, I develop it on my local, not in the company's environment, so I can not access to the network drive, and the sql will be my local sql express. 现在,我在本地而不是在公司环境中开发它,因此我无法访问网络驱动器,而sql将是我的本地sql express。 In this case, is there a good way to configure this, so that, I can simply change a configuration, and it will compile the code for my development environment or for the production? 在这种情况下,是否有一种很好的方法来配置它,以便我可以简单地更改配置,它将为我的开发环境或产品编译代码?

In the myProject.Website, I could add a app key for the development environment, so that it will read the files from my local instead of network drive. 在myProject.Website中,我可以为开发环境添加一个应用程序密钥,这样它将从本地而不是网络驱动器读取文件。 However, the question is: how can I read this key in myProject.Data ? 但是,问题是:如何在myProject.Data中读取此密钥?

Or, is there another way to do this? 或者,还有另一种方法吗?

Now, I develop it on my local, not in the company's environment, so I can not access to the network drive, and the sql will be my local sql express. 现在,我在本地而不是在公司环境中开发它,因此我无法访问网络驱动器,而sql将是我的本地sql express。 In this case, is there a good way to configure this, so that, I can simply change a configuration, and it will compile the code for my development environment or for the production? 在这种情况下,是否有一种很好的方法来配置它,以便我可以简单地更改配置,它将为我的开发环境或产品编译代码?

Normally, connecting to database should be done using a connection string which is configurable. 通常,连接到数据库应使用可配置的连接字符串完成。 So you should have (or define) a configurable key in web.config to represent database connection string and connect using that. 因此,您应该在web.config中具有(或定义)一个可配置的键来表示数据库连接字符串并使用该字符串进行连接。 Furthermore, note that this way, changing the web.config won't "compile" anything and the changes should take effect without the need to rebuild the project. 此外,请注意,以这种方式,更改web.config不会“编译”任何内容,并且更改应在无需重建项目的情况下生效。 see here (section connection string) 请参阅此处 (连接字符串部分)

In the myProject.Website, I could add a app key for the development environment, so that it will read the files from my local instead of network drive. 在myProject.Website中,我可以为开发环境添加一个应用程序密钥,这样它将从本地而不是网络驱动器读取文件。 However, the question is: how can I read this key in myProject.Data ? 但是,问题是:如何在myProject.Data中读取此密钥?

You cannot read the configuration of another project that easily (unless its a class library which is being used in that project or you are willing to read and parse that xml file!) but what's wrong with redefining that key in the second project? 您不能轻易读取另一个项目的配置(除非该项目正在使用其类库,或者您愿意读取并解析该xml文件!),但是在第二个项目中重新定义该键又有什么问题呢?

Generally I think you did not understood web.config's concept very well. 通常,我认为您不太了解web.config的概念。 So following a good tutorial like this may help you master it. 所以下面一个很好的教程像这样可以帮助你掌握它。 Good luck. 祝好运。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM