asp.net-mvc 为什么 .NET 在 MVC asp.net 应用程序中生成两个 web.config 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23509617/
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
Why does .NET generate two web.config files in an MVC asp.net application?
提问by Datta
I am new in MVC 3. What is the reason to use two web.config files?
我是 MVC 3 的新手。使用两个 web.config 文件的原因是什么?


What is difference between that 2 web.config files, and what is the purpose of each and its function?
这两个 web.config 文件有什么区别,每个文件的用途和功能是什么?
采纳答案by Céryl Wiltink
I'd like to add to this that the Web.Config in the /Views folder is a great (if not thé) way to declare namespaces specifically for your views in. In a web application it is very possible almost every view gets a ViewModel (instead of your actual model) passed to it. Declaring the full namespace after @model or having the same @using App.Web.Viewmodels gets tedious. This way, all viewmodels are automatically available and you have to do extra work to get the real models in scope, which should then set of some alarm bells immediatly.
我想补充一点,/Views 文件夹中的 Web.Config 是为您的视图专门声明命名空间的好方法(如果不是这样的话)。在 Web 应用程序中,几乎每个视图都有可能获得一个 ViewModel (而不是您的实际模型)传递给它。在@model 之后声明完整的命名空间或具有相同的@using App.Web.Viewmodels 变得乏味。这样,所有视图模型都会自动可用,您必须做额外的工作才能在范围内获取真实模型,然后应立即设置一些警钟。
Also, usually an application can get a lot of extension-methods specifically for use in the view (the HTML-helper jumps into mind). It makes sense to define the namespace to this extension class in the /Views/Web.Config. That way you never wonder "Why cant IntelliSense find my @Html.ImageLink() method??!"
此外,通常一个应用程序可以获得许多专门用于视图的扩展方法(HTML 助手会跳到脑海中)。在 /Views/Web.Config 中为这个扩展类定义命名空间是有意义的。这样你就永远不会想知道“为什么 IntelliSense 找不到我的 @Html.ImageLink() 方法??!”
回答by StuartLC
This is an example of web.configfile inheritance. From MSDN
这是web.config文件继承的一个例子。来自MSDN
You can distribute ASP.NET configuration files throughout your application directories to configure ASP.NET applications in an inheritance hierarchy. This structure allows you to achieve the level of configuration detail that your applications require at the appropriate directory levels without affecting configuration settings at higher directory levels.
您可以在整个应用程序目录中分发 ASP.NET 配置文件,以在继承层次结构中配置 ASP.NET 应用程序。这种结构允许您在适当的目录级别上实现应用程序所需的配置详细信息级别,而不会影响更高目录级别的配置设置。
Specifically, for MVC projects, the web.configin the Viewsubdirectory is used to tailor the .cshtml/ .aspxfiles. You can use web.configfiles in subfolders to extend, override, and remove settings inherited from the app's own root, and further up the hierarchy, e.g. up to machine.config
具体来说,MVC项目,web.config在View子目录用来裁缝.cshtml/.aspx文件。您可以使用web.config子文件夹中的文件来扩展、覆盖和删除从应用程序自己的根继承的设置,并进一步提升层次结构,例如高达machine.config
Common configurations in the /Views/web.configinclude:
中的常见配置/Views/web.config包括:
- Blocking requests attempting to access razor and aspx views directly (these need to be served from Controllers via the appropriate routes). A
404response is configured for such direct requests, e.g.
- 阻止尝试直接访问 razor 和 aspx 视图的请求(这些需要通过适当的路由从控制器提供)。甲
404响应被配置用于这种直接请求,例如
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
- To setup default import namespacesfor the view pages, which would otherwise have to be explicitly added via
using. You can add namespaces for your common custom assemblies here (e.g. custom html helper extensions) e.g.
- 为视图页面设置默认导入命名空间,否则必须通过
using. 您可以在此处为常用自定义程序集添加命名空间(例如自定义 html 帮助程序扩展),例如
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
...
- To configure the anti-xss
RequestValidationfilter for MVC. The comment added in the config explains this best:
RequestValidation为MVC配置反xss过滤器。配置中添加的注释最好地解释了这一点:
<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
回答by Sunny_Sid
View has its own config. If you are dealing with areas Then you will come to know about more than one config.
视图有自己的配置。如果您正在处理区域,那么您将了解不止一种配置。
Actually The point is that view's Web.Config is for view specifc configutation such as blocking direct access to the views.
实际上重点是视图的 Web.Config 用于视图特定的配置,例如阻止对视图的直接访问。
EDIT 1: More Explaination as asked in comments.
编辑 1:在评论中询问更多解释。
The web.config file exists in the Views folders to prevent access to your views by any means other than your controller. In the MVC design pattern, controllers are supposed to route requests and return a rendered view to the calling client. That means localhost9999://Home/Index.cshtml should not be directly accessible.
web.config 文件存在于 Views 文件夹中,以防止通过控制器以外的任何方式访问您的视图。在 MVC 设计模式中,控制器应该路由请求并将呈现的视图返回给调用客户端。这意味着 localhost9999://Home/Index.cshtml 不应直接访问。
回答by Sunny_Sid
The web.config file exists in the Views folders to prevent access to your views by any means other than your controller. In the MVC design pattern, controllers are supposed to route requests and return a rendered view to the calling client.
web.config 文件存在于 Views 文件夹中,以防止通过控制器以外的任何方式访问您的视图。在 MVC 设计模式中,控制器应该路由请求并将呈现的视图返回给调用客户端。
means localhost9999://Home/Index.cshtmlshould not be directly accessible.
意味着localhost9999://Home/Index.cshtml不应直接访问。
回答by e4rthdog
ASP.NET configuration is stored in web.configfiles (XML files).
ASP.NET 配置存储在web.config文件(XML 文件)中。
These files can appear in many directories in an ASP.NET application. They help to configure application behaviour even before after deploying, based on the fact that you can edit them with notepad. Also they keep separated your code and your configuration data.
这些文件可以出现在 ASP.NET 应用程序的许多目录中。基于您可以使用记事本编辑它们的事实,它们甚至有助于在部署之前配置应用程序行为。他们还将您的代码和配置数据分开。
Every web.configfile applies to to the directory that it exists and ALL the child subdirectories. Web.configfiles in child directory may be used to override the parent's web.configfile.
每个web.config文件都适用于它存在的目录和所有子目录。Web.config子目录中的web.config文件可用于覆盖父目录的文件。
You have the option of overriding individual files or directories by using the location element. See LINK
您可以选择使用 location 元素覆盖单个文件或目录。见链接
The settings inheritance rules are as foillows.
设置继承规则如下。
First there is the machine.config file which is located usually in systemroot\Microsoft.NET\Framework\versionNumber\CONFIG\
首先是 machine.config 文件,它通常位于 systemroot\Microsoft.NET\Framework\versionNumber\CONFIG\
In the same directory exists a 'master' web.config file that defines settings for ALL asp.net application that are running in the machine.
在同一目录中存在一个“master”web.config 文件,该文件定义了机器中运行的所有 asp.net 应用程序的设置。
Then come your web.config files that exist in your application.
然后是您的应用程序中存在的 web.config 文件。
More Info:
更多信息:
ASP.NET configuration overview
回答by Fernando Arce
In addition to distributing your settings for your project simple way you can turn it at runtime to make a publication. One hand on the wheel. See:
除了以简单的方式分发项目设置之外,您还可以在运行时将其转换为发布。一只手放在方向盘上。看:

