asp.net-mvc ASP.NET MVC 和两个 Web.config 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/517086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC and two Web.config files
提问by BuddyJoe
Where is the Web.config supposed to go in an ASP.NET MVC project?
Web.config 应该放在 ASP.NET MVC 项目中的什么位置?
I just ran into an error trying to do this:
我只是在尝试执行此操作时遇到错误:
_cnstr = System.Configuration.ConfigurationManager.
ConnectionStrings["production"].ConnectionString;
The default MVC template puts the Web.config at the root of the project.
默认的 MVC 模板将 Web.config 放在项目的根目录下。
If you go into the properties of a project (the screen with the vertical tabs). Go to settings and try to create an application setting, it will prompt you that you don't have a config file. When it creates the file it does it at the base of the Views folder. So now I have twoWeb.config files. Is this how it supposed to be?
如果您进入项目的属性(带有垂直选项卡的屏幕)。转到设置并尝试创建应用程序设置,它会提示您没有配置文件。当它创建文件时,它会在 Views 文件夹的底部进行。所以现在我有两个Web.config 文件。这是应该的吗?
And I guess I should put my connection string in the "views" web.config to avoid the error. Thoughts? Is this a bug in the last release of the ASP.NET MVC bits?
我想我应该把我的连接字符串放在“视图”web.config 中以避免错误。想法?这是 ASP.NET MVC 位的最新版本中的错误吗?
UPDATE:See David's answer
更新:见大卫的回答
回答by David Tischler
The settings should go into the web.config at the application root. The web.config in the views folder is there to block direct access to the view aspx pages which should only get served through controllers.
这些设置应该进入应用程序根目录下的 web.config。视图文件夹中的 web.config 用于阻止直接访问视图 aspx 页面,这些页面只能通过控制器提供。
(And: I tried creating application settings on my machine, with ASP.NET MVC RC 1 installed, using a newly created mvc web application. They get added to the web.config at the application root.)
(并且:我尝试在我的机器上创建应用程序设置,安装了 ASP.NET MVC RC 1,使用新创建的 mvc Web 应用程序。它们被添加到应用程序根目录的 web.config 中。)

