为什么。net会生成两个web。MVC asp.net应用程序中的配置文件?

时间:2022-06-11 16:04:54

I am new in MVC 3. What is the reason to use two web.config files?

我是MVC 3的新手。使用两个web的原因是什么?配置文件?

为什么。net会生成两个web。MVC asp.net应用程序中的配置文件?

What is difference between that 2 web.config files, and what is the purpose of each and its function?

这两个网页有什么不同?配置文件,它们的目的和功能是什么?

6 个解决方案

#1


20  

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应用程序中,几乎每个视图都有一个ViewModel(而不是您的实际模型)传递给它。在@model之后声明完整的名称空间,或者使用相同的@using App.Web。视图模型变得乏味。通过这种方式,所有的viewmodel都是自动可用的,您需要做额外的工作来获得范围内的真实模型,然后应该立即设置一些警报。

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-helper跳入我的脑海)。在/Views/Web.Config中为这个扩展类定义名称空间是有意义的。这样你就不会想“为什么智能感知不能找到我的@Html.ImageLink()方法?”

#2


49  

This is an example of web.config file inheritance. From MSDN

这是一个网络的例子。配置文件继承。从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。在您的应用程序目录中配置asp.net配置文件以配置ASP。在继承层次结构中的NET应用程序。这种结构允许您在适当的目录级别上实现应用程序所需的配置细节级别,而不会影响更高目录级别的配置设置。

Specifically, for MVC projects, the web.config in the View subdirectory is used to tailor the .cshtml / .aspx files. You can use web.config files 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。视图子目录中的配置用于定制.cshtml / .aspx文件。您可以使用网络。在子文件夹中的配置文件,以扩展、覆盖和删除从应用程序自己的根继承的设置,并在层次结构上更进一步,例如,直到machine.config

Common configurations in the /Views/web.config include:

在/视图/web中的常见配置。配置包括:

  • Blocking requests attempting to access razor and aspx views directly (these need to be served from Controllers via the appropriate routes). A 404 response is configured for such direct requests, e.g.
  • 阻止试图直接访问razor和aspx视图的请求(这些请求需要通过适当的路由从控制器提供)。404响应被配置为此类直接请求,例如。

<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
  • To setup default import namespaces for 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.
  • 为视图页面设置默认的导入名称空间,否则必须通过使用显式添加。您可以在这里为您的公共自定义程序集添加名称空间(例如自定义html帮助程序扩展)。

  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    ...
  • To configure the anti-xss RequestValidation filter for MVC. The comment added in the config explains this best:
  • 为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.
 -->

#3


5  

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设计模式中,控制器应该路由请求并将呈现的视图返回给调用的客户端。

means localhost9999://Home/Index.cshtml should not be directly accessible.

意味着localhost9999:/ / Home /索引。不应该直接访问cshtml。

#4


5  

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。配置用于视图专门化配置,如阻塞对视图的直接访问。

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.

网络。配置文件存在于视图文件夹中,以防止通过除控制器之外的任何方式访问视图。在MVC设计模式中,控制器应该路由请求并将呈现的视图返回给调用的客户端。这意味着localhost9999:/ / Home /索引。不应该直接访问cshtml。

#5


3  

ASP.NET configuration is stored in web.config files (XML files).

ASP。NET配置存储在web中。配置文件(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中的许多目录中。网络应用程序。基于您可以使用记事本对其进行编辑的事实,它们甚至在部署之前就可以帮助配置应用程序行为。它们还将代码和配置数据分开。

Every web.config file applies to to the directory that it exists and ALL the child subdirectories. Web.config files in child directory may be used to override the parent's web.config file.

每一个网络。配置文件适用于它存在的目录和所有子目录。网络。子目录中的配置文件可以用来覆盖父web。配置文件。

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.

设置继承规则就像foillows一样。

First there is the machine.config file which is located usually in systemroot\Microsoft.NET\Framework\versionNumber\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.

在同一目录中存在一个“主”web。配置文件,它定义了在机器中运行的所有asp.net应用程序的设置。

Then come your web.config files that exist in your application.

然后来到你的网站。应用程序中存在的配置文件。

More Info:

更多信息:

ASP.NET configuration overview

ASP。网络配置概述

ASP.NET Configuration file hierarchy and inheritance

ASP。NET配置文件层次结构和继承

#6


1  

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:

除了以简单的方式发布项目设置之外,还可以在运行时将其转换为发布。一只手放在方向盘上。看到的:

Web.config Transformations

网络。配置转换

Transformation Syntax

转换语法

#1


20  

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应用程序中,几乎每个视图都有一个ViewModel(而不是您的实际模型)传递给它。在@model之后声明完整的名称空间,或者使用相同的@using App.Web。视图模型变得乏味。通过这种方式,所有的viewmodel都是自动可用的,您需要做额外的工作来获得范围内的真实模型,然后应该立即设置一些警报。

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-helper跳入我的脑海)。在/Views/Web.Config中为这个扩展类定义名称空间是有意义的。这样你就不会想“为什么智能感知不能找到我的@Html.ImageLink()方法?”

#2


49  

This is an example of web.config file inheritance. From MSDN

这是一个网络的例子。配置文件继承。从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。在您的应用程序目录中配置asp.net配置文件以配置ASP。在继承层次结构中的NET应用程序。这种结构允许您在适当的目录级别上实现应用程序所需的配置细节级别,而不会影响更高目录级别的配置设置。

Specifically, for MVC projects, the web.config in the View subdirectory is used to tailor the .cshtml / .aspx files. You can use web.config files 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。视图子目录中的配置用于定制.cshtml / .aspx文件。您可以使用网络。在子文件夹中的配置文件,以扩展、覆盖和删除从应用程序自己的根继承的设置,并在层次结构上更进一步,例如,直到machine.config

Common configurations in the /Views/web.config include:

在/视图/web中的常见配置。配置包括:

  • Blocking requests attempting to access razor and aspx views directly (these need to be served from Controllers via the appropriate routes). A 404 response is configured for such direct requests, e.g.
  • 阻止试图直接访问razor和aspx视图的请求(这些请求需要通过适当的路由从控制器提供)。404响应被配置为此类直接请求,例如。

<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
  • To setup default import namespaces for 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.
  • 为视图页面设置默认的导入名称空间,否则必须通过使用显式添加。您可以在这里为您的公共自定义程序集添加名称空间(例如自定义html帮助程序扩展)。

  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    ...
  • To configure the anti-xss RequestValidation filter for MVC. The comment added in the config explains this best:
  • 为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.
 -->

#3


5  

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设计模式中,控制器应该路由请求并将呈现的视图返回给调用的客户端。

means localhost9999://Home/Index.cshtml should not be directly accessible.

意味着localhost9999:/ / Home /索引。不应该直接访问cshtml。

#4


5  

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。配置用于视图专门化配置,如阻塞对视图的直接访问。

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.

网络。配置文件存在于视图文件夹中,以防止通过除控制器之外的任何方式访问视图。在MVC设计模式中,控制器应该路由请求并将呈现的视图返回给调用的客户端。这意味着localhost9999:/ / Home /索引。不应该直接访问cshtml。

#5


3  

ASP.NET configuration is stored in web.config files (XML files).

ASP。NET配置存储在web中。配置文件(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中的许多目录中。网络应用程序。基于您可以使用记事本对其进行编辑的事实,它们甚至在部署之前就可以帮助配置应用程序行为。它们还将代码和配置数据分开。

Every web.config file applies to to the directory that it exists and ALL the child subdirectories. Web.config files in child directory may be used to override the parent's web.config file.

每一个网络。配置文件适用于它存在的目录和所有子目录。网络。子目录中的配置文件可以用来覆盖父web。配置文件。

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.

设置继承规则就像foillows一样。

First there is the machine.config file which is located usually in systemroot\Microsoft.NET\Framework\versionNumber\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.

在同一目录中存在一个“主”web。配置文件,它定义了在机器中运行的所有asp.net应用程序的设置。

Then come your web.config files that exist in your application.

然后来到你的网站。应用程序中存在的配置文件。

More Info:

更多信息:

ASP.NET configuration overview

ASP。网络配置概述

ASP.NET Configuration file hierarchy and inheritance

ASP。NET配置文件层次结构和继承

#6


1  

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:

除了以简单的方式发布项目设置之外,还可以在运行时将其转换为发布。一只手放在方向盘上。看到的:

Web.config Transformations

网络。配置转换

Transformation Syntax

转换语法