Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

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

like image 902
Datta Avatar asked May 07 '14 05:05

Datta


People also ask

How many web config file in asp.net MVC application?

There is no restriction to use the web. config file in the asp.net web application. You can have 1 Web. config file per folder .

What is the use of web config file in asp.net MVC?

web. config file is an XML-based configuration file used in ASP. NET-based applications to manage various settings that are concerned with the configuration of our website. In this way, we can separate our application logic from configuration logic.

How many web config file one application can have?

Yes you can have two web. config files in application.

Where is web config file in asp.net MVC?

This file is typically found in the C:\WINDOWS\Microsoft.NET\Framework\v2. 0.50727\CONFIG directory. The Machine. config file contains settings for all sites running on the machine provided another .


1 Answers

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.

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

  <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:

<!--     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.  --> 
like image 135
StuartLC Avatar answered Oct 11 '22 21:10

StuartLC