I am thinking through the best practice for organizing my code in an ASP.NET Core MVC app. I have two types of users, Doctors and Nurses, would it be best practice to organize all my Doctors and Nurses controllers/views in its own Doctors and Nurses sub folders within the controllers/views folder?
Controllers:
Views:
The reason for this is because at each of the Doctors/Nurses sub folder level I want to have its own shared folder for a _viewstart file.
public string Index()
{
return View("~/Views/Doctors/Home/Index.cshtml");
}
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "Doctors/{controller=Home}/{action=Index}/{id?}");
});
_ViewStart.cshtml
for each of the subfolders. Would this work/is it acceptable?Big Thanks to @Craig W. for suggesting Areas. Unlike in older versions of MVC, with Core there is no formal "Areas" option when you right click on the Project. You can however create a folder and name it "Areas". Within the Controllers you can decorate each controller with [Area("Doctors")] to specify the controller for within the Areas.
As far as the startup.cs, I was able to use the following:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areaRoute",
template: "{area:exists}/{controller}/{action}");
routes.MapRoute(
name: "default",
template: "{area=Doctors}/{controller=Home}/{action=Index}");
});
If you wish to use subfolders for your views or partials, the syntax is
public IActionResult Index()
{
return View("~/Views/Doctors/Index.cshtml");
}
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