This project is read-only.

Views in Nested Folders

Mar 13, 2014 at 12:37 PM

i've used MvcCodeRouting to separate my controls and views into namespaces. I also want to separate the files by their physical folders. For the controllers, its working fine, but when i create views with nested folders, i'm getting this error.

"The view '~/SC/MultiEngagementMVC.Views.SC.Home' or its master was not found or no view engine supports the searched locations. The following locations were searched:

My Folder Structure is
MultiEngagementMVC (Root)
| -- Controllers
| --------- SC
| -----------------HomeController.cs
| -- Views
| --------- SC
| -----------------Home
| ----------------------Index.cshtml (renamed to MultiEngagementMVC.Views.SC.Home.cshtml)
| --------- Shared
| ---------_ViewStart.cshtml

Any help appreciated. Please pass me any example where one has used MVCCodeRouting with Nested folders for view. If i create the views at root level, everything is working fine, but the requirement is to create folders by engagement we do.
Mar 13, 2014 at 3:20 PM
The error message suggests you are trying to locate the view using an absolute path, e.g. return View("~/SC/MultiEngagementMVC.Views.SC.Home"), is that the case?
Mar 14, 2014 at 6:58 AM
Yes sir, initially i've tried with returning View(), then some post mentioned that we can use absolute namespace to locate embedded views. When i used View(), it had given a similar error mentioning trying to locate the view in ~/Home, ~/shared and so so. Its a requirement to separate views by their function and has to create views under various sub folders. Please let me know in case i can share anything else.
Mar 14, 2014 at 3:43 PM
Please check this:
  • Make sure you call ViewEngines.Engines.EnableCodeRouting() on Application_Start
  • Are you using embedded views? That renaming you mention (Index.cshtml renamed to MultiEngagementMVC.Views.SC.Home.cshtml) suggests you are. If that's the case you have to use set EnableEmbeddedViews = true at route registration. See Embedded Views for more info.
  • Don't use absolute view paths unless you really mean it. Use View() or View("viewName").
Hopefully that will make it work or at least give you a better error message.