6

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.

5
  • I hate to ask the question and state the obvious but...do you have a really compelling reason to keep using areas? I feel like with just being deliberate and configuring Routes properly you could eliminate the necessity and boom--back down to 1 web.config :) Commented Jun 11, 2016 at 7:57
  • @FernandoRodriguez: The main reason for me to use areas is to organize the files to be easier to navigate for the developers. It's easier for me to have a Controllers folder with 20 controllers in it rather than Controllers folder with 100 controllers in it. The same goes for the Views folder, there would be just too many folders inside of it. But you're right that leaving the idea of areas could be one solution to consider. It currently helps me keep the source code more organized, and I am quite happy with how it works. Commented Jun 11, 2016 at 8:05
  • Yep, makes sense if your project is large enough. Some might argue if you're going to go to that trouble, you could/should have it split it projects with your solution. That's neither here nor there though..Also...instead of using areas, you could simply create named folders (by group or feature or w/e) under Controllers--I have done this in apps with 30+ where I had Api & App functions. So, ~/Controllers/Api/CreateJobController.cs and ~/Controllers/App/JobController.cs for instance. Still get all the organization without the headache! Commented Jun 11, 2016 at 8:11
  • 1
    @FernandoRodriguez: The problem with splitting it into multiple projects is that I'll still have to configure the Views in each one of them separately. Also ASP.NET MVC isn't very prepared for using views from other projects, and I'd rather avoid using solutions like RazorGenerator for various reasons. You're right that creating deeper folder structure in folders like Controllers could be one way of dealing with this. However, I am still quite happy with how the areas work and it makes sense to me, I actually also prefer the longer routes with area in my case. Thanks for your insights! Commented Jun 11, 2016 at 8:20
  • FYI @Tom: remember that all the MVC routes are also overridable/editable, so you're not 100% locked into exactly the way ASP.NET creates routes by default. You have your RouteConfig.cs and then you can also explicitly place the [Route(/Home/blah/)] or [RoutePrefix(/../)] attribute over any given method/controller Commented Jun 12, 2016 at 2:43

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I was just experimenting with this and it seems to work just fine. No caveats to this approach?
Well if you have any special settings that should only apply to one of the areas you can always bring back the individual web.config file to that area,otherwise I don't foresee any problems
Thanks for the insights, I've actually experimented with putting the configuration to the root config, which seems to work fine as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.