Why does .NET generate two web.config files in an MVC asp.net application?

后端 未结 6 1730
一整个雨季
一整个雨季 2020-12-05 04:16

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

\"enter

Wha

6条回答
  •  借酒劲吻你
    2020-12-05 05:09

    This is an example of web.config file inheritance. From 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.

    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

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

    • 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.

    
    
    • 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.

      
        
        
        
        ...
    
    • To configure the anti-xss RequestValidation filter for MVC. The comment added in the config explains this best:

    
    

提交回复
热议问题