MazeezaM
MazeezaM

Reputation: 149

Using MVC 4 areas at custom path

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

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:

The path I want it to find is

Also if I had to use

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

I would expect it to finde

Upvotes: 1

Views: 902

Answers (2)

MazeezaM
MazeezaM

Reputation: 149

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.

Upvotes: 0

Jamie
Jamie

Reputation: 3051

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.

Upvotes: 1

Related Questions