asp.net-mvc MVC项目的views文件夹中的Web.Config文件有什么作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6204341/
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
What does the Web.Config file do in the views folder of a MVC project
提问by Ola Karlsson
I'm having some problems with deploying my application and while troubleshooting, I came across the Web.Config
file in the Views
folder. In an attempt to narrow down the possibilities of sources to my problem, I tried to find out the purpose of that ~Web.Config` file but can't really find much information.
我在部署应用程序时遇到了一些问题,在进行故障排除时,我遇到了Web.Config
文件Views
夹中的文件。为了缩小我的问题来源的可能性,我试图找出 ~Web.Config` 文件的目的,但实际上找不到太多信息。
So basically my questions are:
所以基本上我的问题是:
- What does the
Web.config
file do in theViews
folder of a MVC project? - Is it required?
- 该
Web.config
文件在Views
MVC项目的文件夹中有什么作用? - 是必需的吗?
In Asp.Net webforms, I believe that to use a separate web.config file in a folder, that folder has to be set as a virtual folder in IIS. Is this the case in MVC (i.e. does the Views
folder need to be configured as a virtual folder)?
在 Asp.Net webforms 中,我相信要在文件夹中使用单独的 web.config 文件,必须将该文件夹设置为 IIS 中的虚拟文件夹。MVC 是这种情况Views
吗(即文件夹是否需要配置为虚拟文件夹)?
采纳答案by David Fox
No, you do not need to configure a virtual folder because of this extra web.config file.
不,由于这个额外的 web.config 文件,您不需要配置虚拟文件夹。
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 设计模式中,控制器应该路由请求并将呈现的视图返回给调用客户端。
In other words, your view at www.mydomain.com/MySuperController/AwesomeAction1/SweetPage.aspx should notbe directly accessible.
换句话说,你在www.mydomain.com/MySuperController/AwesomeAction1/SweetPage.aspx视图应该不是直接访问。
If you peek at the web.config file it actually registers the HttpNotFoundHandler
to all paths and verbs:
如果您查看 web.config 文件,它实际上HttpNotFoundHandler
将所有路径和动词注册了:
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
Or, in IIS 7 it might look like
或者,在 IIS 7 中它可能看起来像
<add name="BlockViewHandler" path="*.aspx" verb="*"
preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>
回答by Daniel A. White
It configures the compiler for the views such as importing namespaces and makes the views folder return a 404.
它为视图配置编译器,例如导入命名空间,并使视图文件夹返回 404。
回答by Prajwal
The web.config file in the views folder is to do some specialized settings you want to apply to pages inside the view folder.
视图文件夹中的 web.config 文件是做一些你想要应用到视图文件夹内的页面的专门设置。
Like config settings like: connection string / appsettings etc.
像配置设置,如:连接字符串/appsettings 等。
but that will be applicable to only that folder and rest of the project will pick up the settings from web.config present at the root.
但这仅适用于该文件夹,项目的其余部分将从根目录下的 web.config 中获取设置。
Specially when you use concept of area there will be separate folder for each area containing separate web.cfg file where you can apply separate settings for each area.
特别是当您使用区域概念时,每个区域都会有单独的文件夹,其中包含单独的 web.cfg 文件,您可以在其中为每个区域应用单独的设置。
回答by Ken D
That's if you want to override something mentioned in the upper web.config
, i.e. if you want to customize something within the scope of the Views
folder.
那就是如果你想覆盖在 upper 中提到的东西web.config
,即如果你想自定义Views
文件夹范围内的东西。