在哪里写连接字符串?在app.config或web.config中? [英] where to write the connection string ? in app.config or in web.config?

查看:69
本文介绍了在哪里写连接字符串?在app.config或web.config中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发MVC应用程序.我的应用程序中有两个项目.一个是MVC应用程序包含,Controller和Views,另一个是DataLayer项目.

I am developing MVC application. I have two projects in my application. one is MVC application contains, Controller and Views and the second one is DataLayer project.

我对在哪里写连接字符串感到困惑,因为在发布应用程序时,它需要使用web.config文件,并且我从DataLayer项目中获取数据,因此我应该在app.config/Web.config中添加连接字符串吗?数据层项目?

I am confused about the where to write the connection string, because while publishing the application it takes the web.config file and I get the data from DataLayer project so should I add connection string in the app.config/Web.config of Data layer project ?

还想知道app.config和web.config的目的和区别是什么?

Also, would like to know what is the purpose and difference between app.config and web.config ?

推荐答案

每个项目在创建时都附带一个配置文件.通用类库具有称为 app.config 的通用类库.Web项目是更特定的,因此其文件名为 web.config 并带有特定于Web的参数.它们都具有相同的目的.

Every project comes with a configuration file when it's created. A general class library has a generic one called app.config. A web project is more specific, so its file is called web.config and comes with web-specific parameters. They both serve the same purpose.

您面临的问题是,默认情况下仅部署可执行项目的配置文件(web.config).您有几种选择:

The problem you are facing is that only the executable project's config file (web.config) is deployed by default. You have a few options:

  1. 将连接字符串添加到web.config并将其传递到数据层.这很简单(最常见),但是将配置信息与数据层项目分开.
  2. 让您的数据层使用 System.Configuration.ConfigurationManager 读取web.config.这可以减轻您将数据传递到数据层的麻烦,但是会产生强烈的依赖性​​(如果没有正确格式化的web.config文件,您的数据层将无法工作).
  3. 将app.config部署为XML内容并编写自定义代码,以便您的数据层可以读取它.这是更多的工作,但是它将您的数据配置从Web配置中删除.
  4. 对#2稍作更改,您可以创建自定义配置部分在web.config中称为"dataLayer".可以通过 System.Configuration.ConfigurationManager 读取.我喜欢这种方法,因为这似乎是一个很好的平衡.在默认"配置文件中,有一个自定义的强类型配置部分.
  1. Add your connection string to web.config and pass it to your data layer. This is simple (an most common), but separates the config info from your data layer project.
  2. Have your data layer read web.config using System.Configuration.ConfigurationManager. This alleviates you from passing the data to your data layer, but creates a strong dependency (your data layer will not work without a properly formatted web.config file).
  3. Deploy app.config as XML content and write custom code so that your data layer can read it. This is more work, but it gets your data config out of the web config.
  4. A slight change to #2, you can create a custom config section called "dataLayer" in web.config. This can be read via System.Configuration.ConfigurationManager. I prefer this approach, as it seems like a good balance. You have a custom strongly-typed config section in the "default" config file.

这个与相关的问题也有一些很好的信息.

This related question has some good info too.

这篇关于在哪里写连接字符串?在app.config或web.config中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆