Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why are there 2 web.config files

Why are there multiple (2) web.config files:

  • 1 in the root directory
  • 1 in the views directory
like image 541
leora Avatar asked Aug 31 '09 00:08

leora


People also ask

Why are there two web config files in MVC?

So logically if the view folder and view (webpages) are accessible or can be requested by user then the basic MVC pattern and practice followed by Microsoft will be violated. This is prevented by adding a web. config and blocking access to view folder and files directly via user request or through URL.

Can we have 2 web config files?

Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration. For example if you have a application which has two modules lets say accounts and sales.

How do I use different web config files?

config (or any file) when you press F5 in Visual Studio. You can have different transformations based on the build configuration. This will enable you to easily have different app settings, connection strings, etc for Debug versus Release. If you want to transform other files you can do that too.


2 Answers

The web.config in the Views directory just has one significant entry, which blocks direct access:

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

This is so someone cannot manually try to go to http://www.yoursite.com/views/main/index.aspx and load the page outside the MVC pipeline.

like image 114
Rex M Avatar answered Sep 23 '22 03:09

Rex M


What Silky said, except reworded.

In ASP .NET there is basically an inheritance style thing going on for config files. You have a machine.config out there in the .net framework folder which has basic settings for all apps on the machine. Anything you specify in a root web.config with the same tags would override the stuff in the machine.config.
Any web.config in a sub-folder can override or add additional settings within that sub-folder and its children.

It's always fun for me the first time one of my newer programmers puts in a http handler in a root folder and then all of the apps in the virtual directories under it explode because they don't have the DLL (they should have put the http handler statement only in the app that needed it, not in the root). :)

like image 37
Eric Avatar answered Sep 24 '22 03:09

Eric