Views in Nested Folders

Mar 13, 2014 at 11:37 AM
Hi,

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:
~/SC/MultiEngagementMVC.Views.SC.Home".

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.
Coordinator
Mar 13, 2014 at 2: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 5: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.
Coordinator
Mar 14, 2014 at 2: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.