1
votes

I'm trying to use areas at a custom path, and I'm having issues. I've been googeling a bunch, but havent found a solution.

My project is a EPiServer CMS project (which shouldn't have any effect I think, just wanna mention it, in case it does)

My structure is

  • Root
    • CompanyName
      • Areas
        • Commerce
          • Controllers
          • Models
          • Views
        • Cms
          • Controllers
            • HomePageController
          • Models
          • Views
            • HomePage
              • Index.cshtml
So I have a layer more to the tree then 'normal' which is the 'CompanyName'

I have this in global.asax.cs

protected void Application_Start()
{
    ViewEngines.Engines.Add(new AreaTemplateViewEngineDynamic());
    AreaRegistration.RegisterAllAreas();
    ...
}

I have a Custom RazorEngine (Could have just added more paths to the default, but have this solution as of now)

public class AreaTemplateViewEngineDynamic : RazorViewEngine
{
    public AreaTemplateViewEngineDynamic()
    {
        this.PartialViewLocationFormats = this.ViewLocationFormats = this.MasterLocationFormats =
                new string[]
                {
                    "~/CompanyName/Views/{1}/{0}.cshtml", "~/CompanyName/Views/Shared/{0}.cshtml"
                };

        this.AreaMasterLocationFormats = this.AreaPartialViewLocationFormats = this.AreaViewLocationFormats =
                new string[]
                {
                    "~/CompanyName/Areas/{2}/Views/{1}/{0}.cshtml", "~/CompanyName/Areas/{2}/Views/Shared/{0}.cshtml"
                };
    }
}

Adding this area registration

public class CmsAreaRegistration: AreaRegistration
{
    public override string AreaName
    {
        get { return "Commerce"; }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Cms_default",
            "Cms/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            namespaces: new[] { "Root.CompanyName.Areas.Cms.Controllers" }
        );
    }
}

When I try to load the page, it seems it doesnt look at the Area paths, only the non-area paths.

The view 'index' or its master was not found or no view engine supports the searched locations. The following locations were searched:

  • ~/Views/HomePage/index.aspx
  • ~/Views/HomePage/index.ascx
  • ~/Views/Shared/index.aspx
  • ~/Views/Shared/index.ascx
  • ~/Views/HomePage/index.cshtml
  • ~/Views/HomePage/index.vbhtml
  • ~/Views/Shared/index.cshtml
  • ~/Views/Shared/index.vbhtml
  • ~/CompanyName/Views/HomePage/index.cshtml
  • ~/CompanyName/Views/Shared/index.cshtml

The path I want it to find is

  • ~/CompanyName/Areas/Cms/Views/HomePage/index.cshtml

Also if I had to use

@{Html.RenderAction("MiniCart", "Cart", new { area = "Commerce"} );}

I would expect it to finde

  • ~/CompanyName/Areas/Commerce/Views/Cart/MiniCart.cshtml
2

2 Answers

1
votes

You are only setting the location for the AreaMasterLocation when you should also set the following locations:

  • AreaPartialViewLocationFormats
  • AreaViewLocationFormats

Find the following class in the object browser: VirtualPathProviderViewEngine for more properties and methods.

0
votes

I wrote my own RazorViewEngine, where I added some custom codes to finding paths. Could just use the URL, because the URL was controlled by the CMS, so the URL didnt represent the MVC path.