I am trying to setup a directory hierarchy like the following :

  • Areas
    • Agents
      • Views
        • Billing
          • BillingInquiry
            • Index.cshtml
          • BillingLookup
            • Index.cshtml
        • Policy
          • PolicyInquiry
            • Index.cshtml
          • PolicyLookup
            • Index.cshtml

As you can see I essentially have Areas within Areas. My AgentsAreaRegistration RegisterArea looks like this :

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            name: "Billing",
            url: "Agents/Billing/{controller}/{action}/{id}",
            defaults: new { controller = "BillingInquiry", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "RAM15MVC.Areas.Agents.Controllers.BillingControllers" }
        );

        context.MapRoute(
            name: "Policy",
            url: "Agents/Policy/{controller}/{action}/{id}",
            defaults: new { controller = "PolicyInquiry", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "RAM15MVC.Areas.Agents.Controllers.PolicyControllers" }
        );

        context.MapRoute(
            name: "Agents_default",
            url: "Agents/{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "RAM15MVC.Areas.Agents.Controllers" }
        );
    }

I am also using a custom razor view engine that looks like this :

public class RDDBViewEngine : RazorViewEngine
{
    private static string[] NewPartialViewLocationFormats = new[] { 
        "~/Views/Shared/ViewMenus/{0}.cshtml",
        "~/Areas/Agents/Views/Shared/ViewMenus/{0}.cshtml",
        "~/Areas/Policyholders/Views/Shared/ViewMenus/{0}.cshtml"
    };

    private static string[] NewAreaViewLocationFormats = new[] {
        "~/Areas/Agents/Views/Policy/{1}/{0}.cshtml",
        "~/Areas/Agents/Views/Billing/{1}/{0}.cshtml"
    };

    public RDDBViewEngine()
    {
        base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewLocationFormats).Union(NewAreaViewLocationFormats).ToArray();
        base.AreaViewLocationFormats = base.AreaViewLocationFormats.Union(NewAreaViewLocationFormats).ToArray();
    }
}

Within the PolicyInquiryController if no id is passed then I redirect them to the PolicyLookup like this :

return RedirectToAction("Index", "PolicyLookup");

When I do this MVC is trying to take them to /Areas/Agents/Billing/PolicyLookup. Here is a screenshot of Route Debugger :

enter image description here

So far it seems I can get this to work by using a RedirectToRoute rather than RedirectToAction like this :

return RedirectToRoute("Policy", new RouteValueDictionary { { "action", "Index" }, { "controller", "PolicyLookup" });

But do I need to do this?

Am I doing something wrong here or with this directory structure or do I need to use RedirectToRoute on everything? Is there something wrong with my mapped routes, view engine or redirects? I've been beating my head against the wall on this for a while now and have tried many things. In general it seems like an awful lot of work just to get views inside subfolders.

Thanks for any help in advance!

Related posts

Recent Viewed