What does the Web.Config file do in the views folder of a MVC project

asp.net MvcWeb Config

asp.net Mvc Problem Overview


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.

So basically my questions are:

  1. What does the Web.config file do in the Views folder of a MVC project?
  2. 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 Mvc Solutions


Solution 1 - asp.net Mvc

No, you do not need to configure a virtual folder because of this extra web.config file.

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.

In other words, your view at www.mydomain.com/MySuperController/AwesomeAction1/SweetPage.aspx should not be directly accessible.

If you peek at the web.config file it actually registers the HttpNotFoundHandler to all paths and verbs:

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

Or, in IIS 7 it might look like

<add name="BlockViewHandler" path="*.aspx" verb="*" 
    preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>

Solution 2 - asp.net Mvc

It configures the compiler for the views such as importing namespaces and makes the views folder return a 404.

Solution 3 - asp.net Mvc

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.

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.

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.

Solution 4 - asp.net Mvc

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOla KarlssonView Question on Stackoverflow
Solution 1 - asp.net MvcDavid FoxView Answer on Stackoverflow
Solution 2 - asp.net MvcDaniel A. WhiteView Answer on Stackoverflow
Solution 3 - asp.net MvcPrajwalView Answer on Stackoverflow
Solution 4 - asp.net MvcKen DView Answer on Stackoverflow