Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One Web.config in ASP.NET MVC instead of multiple for each Views folder

My ASP.NET MVC web application has 5 areas. Each area has its own Views folder with it's own Web.config in it. That makes 6 configuration files together with the main Web.config at the application root. It makes it a bit difficult to manage.

As far as I know, these configs make two things (at least by default):

1.) Setup the Razor host factory to include selected namespaces by default.

2.) Block direct access to the files in the Views folder by handling the requests with a HttpNotFoundHandler.

The code in all of those Web.config files is almost identical for me and it doesn't seem to me like a good way to manage these configurations. If I added more areas or organized my code more granularly, I could end up with more that just 5 Web.config files. Why would I need so many? Isn't one enough?

My thoughts on the points are following:

ad 1.) Cannot all of these namespaces be imported either once in the application root Web.config file instead or imported in a _ViewStart.cshtml file?

ad 2.) Wouldn't it make more sense to block access to all code files in all folders and then use the opposite approach -- i.e. change the folders with static files in them (like Assets folder) to use StaticFileHandler? In principle, it seems like a more secure approach to me to whitelist the files that are allowed be read by StaticFileHandler rather than to specify which files should be handled by HttpNotFoundHandler.

Is this doable? Am I missing something? Has anybody else encountered similar issues with having too many Views Web.config files?

Thanks for answers.

like image 759
Tom Pažourek Avatar asked Jun 11 '16 07:06

Tom Pažourek


People also ask

Can we have multiple Web config files in MVC?

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.

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 you have multiple Web config files?

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 in view folder in MVC?

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.


1 Answers

Just place a single web.config file inside the Areas folder and remove the individual web.config files from each of the 5 areas.

enter image description here

Configuration files in ASP.NET operate on a principle of inheritance. MSDN article - ASP.NET Configuration File Hierarchy and Inheritance

like image 149
Denys Wessels Avatar answered Oct 16 '22 13:10

Denys Wessels