0

I have a MVC 5 project with lots of controllers, models and pages. For making them more maintainable i moved some of related files to a new area, called Mail.

The project structure is like:

Automation (project's name)
 |- Controllers
 |- Views
 |- Models
 └── Areas  
      └── Mail
          └── Models 
          └── Controllers
               └── MailboxController
          └── Views
               └── Inbox.cshtml

I did below steps to be able to load newly moved views:

  • Change namespace of the controller in the Area -> Mail -> Controllers
  • Add Rout to the MailAreaRegistaration.cs as follow:

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Mail_default",
            "Mail/{controller}/{action}/{id}",
            new { action = "Inbox", id = UrlParameter.Optional},
            new [] { "Automation.Areas.Mail.Controllers" });
    }
    
  • RegisterAllAreaas() in the global.asax.cs
  • Change MapRout method in the project's RoutConfig file as below:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Dashboards", action = "Dashboard_Main", id = UrlParameter.Optional },
            namespaces: new [] {"Automation.Controllers"}
        );
    }
    

All the pages that are in the main View folder rendered as expected, but when i want to load the inbox.cshtml in the Mail area this errors are shown:

The view 'inbox' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Areas/Mail/Views/mailbox/inbox.aspx
~/Areas/Mail/Views/mailbox/inbox.ascx
~/Areas/Mail/Views/Shared/inbox.aspx
~/Areas/Mail/Views/Shared/inbox.ascx
~/Views/mailbox/inbox.aspx
~/Views/mailbox/inbox.ascx
~/Views/Shared/inbox.aspx
~/Views/Shared/inbox.ascx
~/Areas/Mail/Views/mailbox/inbox.cshtml
~/Areas/Mail/Views/mailbox/inbox.vbhtml
~/Areas/Mail/Views/Shared/inbox.cshtml
~/Areas/Mail/Views/Shared/inbox.vbhtml
~/Views/mailbox/inbox.cshtml
~/Views/mailbox/inbox.vbhtml
~/Views/Shared/inbox.cshtml
~/Views/Shared/inbox.vbhtml

I tested below urls for inbox page, but no one was successful

How can i fix it? Thanks

Ehsan Toghian
  • 548
  • 1
  • 6
  • 26

2 Answers2

0

Assumed you have MailboxController class like this:

public class MailboxController : Controller
{
    // default action in area
    public ActionResult Inbox()
    {
        return View();
    }
}

First, create custom class to perform view search in area folders given by example below:

namespace Automation
{    
    public class CustomRazorEngine : RazorViewEngine
    {
        public CustomRazorEngine()
        {
            // short note:
            // {2} = area name
            // {1} = controller name
            // {0} = action name
            AreaViewLocationFormats = new[]
            {
                // add these routes in area scope
                "~/Areas/{2}/Views/{0}.cshtml",
                "~/Areas/{2}/Views/{1}/{0}.cshtml",
            };

            ViewLocationFormats = new[]
            {
                // example routes
                "~/{1}/{0}.cshtml",
                "~/Views/{1}/{0}.cshtml"
            }

            FileExtensions = new[]
            {
                "cshtml"
            };
        }
    }
}

Then I register customized view engine as described above in Application_Start method inside Global.asax:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    ViewEngines.Engines.Add(new CustomRazorEngine()); // customized view search engine
    // other settings here
}

With setup above those URLs below should work:

http://localhost:port/Mail/Mailbox
http://localhost:port/Mail/Mailbox/Inbox

References:

Custom View Engine with Dynamic View Location

ASP.NET MVC Custom View Routing

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
0

The simplest way to fix this is to follow the MVC conventions and to move the Inbox.cshtml file to the Areas\Mail\Views\Mailbox\Inbox.cshtml location that is expected by MVC (you are only missing the Mailbox folder).

However, if your intention is to customize the view location to your whim, use Tetsuya's solution.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212