Why there are two Web. config in an MVC project

Source: Internet
Author: User

We are not unfamiliar with MVC, when creating an MVC project, we will always find that there are two Web. config files in the project directory, one in the project root directory, and under Views, the contents of the two Web. config are different, so why are there two Web. config file, which is what this essay will explain.

    1. Web. config file under the root directory

The Web. config file under the root directory works for all files under the root directory, and when you open the Web. config file under the root directory, you will find that web. Httpnotfoundhandler registered for all paths or actions.

<add path= "*" verb= "*" type= "System.Web.HttpNotFoundHandler"/>

Or in IIS7, it might be so defined.

<add name= "Blockviewhandler" path= "*.aspx" verb= "*"     precondition= "Integratedmode" type= " System.Web.HttpNotFoundHandler "/>

In addition, you can add custom error pages, or some configuration information, such as database connection, key-value equivalence, some of the configuration in the project is read from the Web. config in the root directory. Some of the necessary assemblies are also registered under the root directory, and when the project is run, the MVC framework loads according to the assembly file that is registered in the Web. config file under the root directory, including the version number of the assembly, the key, and so on.

  2. Web. config file under views

The Web. config under Views is primarily a view that prevents access to views under the View folder by means other than the Controller, and in MVC's design pattern, Controllers supports routing requests, and returns a specific rendered view to the calling client.

If you want to control the views under the view, then you have to add some special settings in the Web. config file under viewing, if you introduce the concept of area in the project (add area), then each area will contain a special web. config text So that you can have some special control over each area.

Views under the view, essentially a front-end class, it will be the first time our view engine to find it to compile it into a front-end page class, after compiling, we need to inherit the front-end page Class A webviewpage<tmodel> generic class, or the Webviewpage class (note: The Webviewpage class is the parent class of the Webviewpage<tmodel> Class), and the Web. config in this directory specifies the parent class of the front-end page class that is generated after the view is compiled

Open the Web.congig file and you will see the following code;

    <pages pagebasetype= "System.Web.Mvc.WebViewPage" >      <namespaces>        <add namespace= " SYSTEM.WEB.MVC "/>        <add namespace=" System.Web.Mvc.Ajax "/> <add namespace=        " System.Web.Mvc.Html "/>        <add namespace=" System.Web.Routing "/>      </namespaces>    </pages>

If you delete the Web. config file under views, the following error is reported

This is because when you visit a view page, such as/home/index, the View engine compiles the index view, and when it is compiled, it finds that the view is found, and it does not know who the parent class of the front-end page class is after compiling.

This is why the Web. config file under the View folder is about telling the view engine how to compile the view's foreground page class, and the parent class for the current page class is Webviewpag, in general, The configuration in this web. config does not require us to manually change anything, nor can we delete

Why there are two Web. config in an MVC project

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.