I don't like ASP.NET MVC's default folder structure
I have been using ASP.NET MVC for about four years in production now. My team built two typical e-commerce websites for the company StockFood that I’m working for. While I was very happy about ASP.NET MVC in the beginning, I began to see it’s weeknesses more and more in the last two years.
The first problem, that our development team had to face after some time, is the default folder structure of ASP.NET MVC:
+ Controller
- HomePageController.cs
+ Views
- Home
- Index.cshtml
+ Model
- Home
- IndexViewModel.cs
There’s one folder for controllers, one for views and one for (view) models. Controllers, views and view models get separted from each other, altough they belong together. From my point of view, they belong together, because you need all of them for a particular page and none of these elements is reused by another component. However, by default, you have to split them up.
If you have a lot of pages/controllers, it becomes very hard to navigate to the components
you want to edit. I guess that’s also why the tooling for ASP.NET MVC Visual Studio includes
things like go to view
in the context menu. Which by the way also only works, if you have
one view per action, and the view name is equal to the name of the action.
I realized, that it is much easier for us, if the elements, which belong together are also stored together. This way you don’t have to search for related elements (like the corresponding) view for the action you are currently working with.
In our codebase these elements therefore are stored like this:
+ Controllers
+ HomePage
- HomePageController.cs
- Index.cshtml
- IndexViewModel.cs
- BannerImage.jpg
- HomePage.css
- HomePage.js
+ AnotherPage
+ YetAnotherPage
With this structure you can also easily see, to which unit of the website a file belongs. If we want to delete a page, we can delete the whole folder with all the elements which are explicitly used by that page.
Unfortunately it’s not as easy as it should be, to change they way, that ASP.NET MVC resolves view names to pathes. Another thing, that I don’t like that much.