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。视图文件夹中的配置文件。为了缩小我的问题来源的可能性,我试图找出那个~网络的目的。配置文件,但不能找到很多信息。
So basically my questions are:
所以基本上我的问题是:
- What does the
Web.config
file do in theViews
folder of a MVC project? - 什么网络。配置文件在MVC项目的视图文件夹中做什么?
- Is it required?
- 它是必需的吗?
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。配置文件在文件夹中,该文件夹必须设置为IIS中的一个虚拟文件夹。这是MVC中的情况吗(例如,视图文件夹是否需要配置为一个虚拟文件夹)?
4 个解决方案
#1
70
No, you do not need to configure a virtual folder because of this extra web.config file.
不,由于这个额外的web,您不需要配置虚拟文件夹。配置文件。
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.
网络。配置文件存在于视图文件夹中,以防止通过除控制器之外的任何方式访问视图。在MVC设计模式中,控制器应该路由请求并将呈现的视图返回给调用的客户端。
In other words, your view at www.mydomain.com/MySuperController/AwesomeAction1/SweetPage.aspx should not be 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:
如果你看一下网络。配置文件它实际上将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"/>
#2
6
It configures the compiler for the views such as importing namespaces and makes the views folder return a 404.
它为视图配置编译器,例如导入名称空间,并使视图文件夹返回404。
#3
5
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.
这是如果您想要覆盖上面web中提到的内容。配置,也就是说,如果您想要自定义视图文件夹范围内的内容。
#4
5
The web.config file in the views folder is to do some specialized settings you want to apply to pages inside the view folder.
网络。视图文件夹中的配置文件将执行一些您想要应用于视图文件夹中的页面的特殊设置。
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上获取设置。配置显示在根中。
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文件中,您可以为每个区域应用单独的设置。
#1
70
No, you do not need to configure a virtual folder because of this extra web.config file.
不,由于这个额外的web,您不需要配置虚拟文件夹。配置文件。
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.
网络。配置文件存在于视图文件夹中,以防止通过除控制器之外的任何方式访问视图。在MVC设计模式中,控制器应该路由请求并将呈现的视图返回给调用的客户端。
In other words, your view at www.mydomain.com/MySuperController/AwesomeAction1/SweetPage.aspx should not be 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:
如果你看一下网络。配置文件它实际上将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"/>
#2
6
It configures the compiler for the views such as importing namespaces and makes the views folder return a 404.
它为视图配置编译器,例如导入名称空间,并使视图文件夹返回404。
#3
5
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.
这是如果您想要覆盖上面web中提到的内容。配置,也就是说,如果您想要自定义视图文件夹范围内的内容。
#4
5
The web.config file in the views folder is to do some specialized settings you want to apply to pages inside the view folder.
网络。视图文件夹中的配置文件将执行一些您想要应用于视图文件夹中的页面的特殊设置。
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上获取设置。配置显示在根中。
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文件中,您可以为每个区域应用单独的设置。