I would like to know how can I do to structure a web application using one folder per feature (Customers, Orders) instead of one folder per artifact type (Controllers, Views); this seems to be a much better way to organize large projects with lots of features, but I can't find any information about it.
I don't think that using Areas would be a solution because using one Area per-feature would require creating lots of nested folders.
I think that what I want to do should be possible by customizing a IViewEngine implementation, but I'm not sure about that.
Has anyone tried to do this?
It shouldn't! That's why it's designed in a modular way. In most web applications out there, we version and deploy all these assemblies (Web, BLL and DAL) together. So, separating a project into 3 projects does not add any values.
Unless there is a better place to put them, I stick them in the Model folder. If you have a lot of Enums though, you might want to do the folder idea that you were doing.
The primary difference between ASP.NET MVC and ASP.NET Core is their cross-platform approach. ASP.NET Core can be used on Windows, Mac, or Linux, whereas ASP.NET MVC can only be used for applications on Windows.
Hence, basically models are business domain-specific containers. It is used to interact with database. It can also be used to manipulate the data to implement the business logic. Let's take a look at a simple example of Model by creating a new ASP.Net MVC project. Step 1 − Open the Visual Studio.
You can create root Features folder, create one folder for each of your features and Shared folder within it. Then you can add all files (controllers, models, views, scripts) related to a single feature to it's folder. If multiple features use same file you can it to Shared folder. This is how project structure may look like:
- App
- Features
- Orders
- OrdersController.cs
- Create.cshtml
- Create.js
- CreateModel.cs
- Edit.cshtml
- Edit.js
- EditModel.cs
- EditViewModel.cs
...
- Customers
...
- Shared
- _Layout.cshtml
- Error.cshtml
- _ViewStart.cshtml
- Web.config
- FeatureViewLocationRazorViewEngine.cs
...
- Web.config
In order to use Razor with this folder structure you need to create new view engine class inherited from RazorViewEngine
, set proper ViewLocationFormats
, MasterLocationFormats
, PartialViewLocationFormats
and add instance of your view engine to ViewEngines.Engines
collection. For sample implementation and it's usage check FEATURE FOLDERS IN ASP.NET MVC article by Tim G. Thomas.
If you want to use classes like Scripts
in your views you also need to import their namespaces. One way to do this is to copy Web.config file from old Views folder to Features folder. For another options check How do I import a namespace in Razor View Page? question.
For more information check following articles:
You can change the location of where views are stored, if you would like. An example, http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.asp.
ASP.NET MVC is easy to develop with because of the philosophy of convention over configuration. If you really want to change those conventions, you can; however, you will find yourself doing a lot more coding. For example, scaffolding will not work with your configuration.
Why not just create your logical separations inside the artifact folders? So, inside your Models folder have a folder for ViewModels and one for DataModels. Inside the DataModels folder, create folders for the different subsets of models (Customers, Orders, etc.). Just my 2 cents.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With