Tom Pažourek
Tom Pažourek

Reputation: 10177

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.

Upvotes: 6

Views: 1916

Answers (1)

Denys Wessels
Denys Wessels

Reputation: 17049

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

Upvotes: 7

Related Questions