1

I need my ASP.NET MVC5 view structure to reflect the structure of my controllers.


The controllers of my application have the following structure:

Controllers
|--- Main
|    |--- HomeController.cs
|--- User
|    |--- HomeController.cs
|    |--- SettingsController.cs
|--- Admin
|    |--- HomeController.cs
|    |--- ManageController.cs

The controller structure is based on the user type, and the Main controllers are accessible to all users. My URLs are like this:

  • /Main/Home (default)
  • /User/Settings
  • /Admin/Manage/Users

But as you can see all namespaces have a HomeController, but when I want to create, for example, an Index view for User/HomeController it will be placed inside Views/Home/Index.cshtml. But I actually need this view to be here: Views/User/Home/Index.cshtml. Below an example structure of how I need the views to be structured.

Views
|--- Main
|    |--- Home
|    |    |--- Index.cshtml
|    |    |--- Contact.cshtml
|--- User
|    |--- Home
|    |    |--- Index.cshtml
|    |--- Settings
|    |    |--- Index.cshtml
|--- Admin
|    |--- Home
|    |    |--- Index.cshtml
|    |--- Manage
|    |    |--- Index.cshtml
|    |    |--- Users.cshtml

I tried using a custom RazorViewEngine but all examples I could find (like this one) wouldn't work for my situation. Is there a way to achieve this specific structure with custom routing?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Duncan Lukkenaer
  • 12,050
  • 13
  • 64
  • 97

3 Answers3

2

I belive you're looking for areas.

Areas was avalable in MVC 4. And there is no difference in MVC 5.

Here you can find a details.

teo van kot
  • 12,350
  • 10
  • 38
  • 70
1

As others have pointed out, you could use Areas to structure your website (provided you only want 1 level).

If you want multiple levels of controllers (/section1/sectiona/SomeController.cs), then have a look at MvcCodeRouting.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
0

You can specify whatever path you need instead of using defaults if areas don't work for you :

return View("user/home/index", model);
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179