jbogard/ContosoUniversity

Areas

kengineer opened this issue · 3 comments

How would you go about adding support for Areas? I think it would make sense for a folder structure similar too

  • Areas
    • AreaName
      • Features
        • Foo
          • Index.cshtml
          • UiController.cs

You'd have to fix up the controller convention to instantiate the right controller.

Areas in MVC are kinda broken though, they were bolted on later so they're a bit funny to get to work outside of the default scenario.

Thanks. I figured out that I need to modify the ControllerFactory, but figuring out the routing for the Areas is where I'm currently stuck, albeit, I haven't spent too much time at it yet. When I have a little more time, I'll take another crack at it.

I got this working. I adding the following area location formats to the FeatureViewLocationRazorViewEngine class constructor

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

and modified the ControllerFactory.GetControllerType method like so

protected override Type GetControllerType(RequestContext requestContext, string controllerName)
{
    var tokens = requestContext.RouteData.DataTokens;
    var areaName = tokens["area"] as string;
    var areaNamespace = !String.IsNullOrEmpty(areaName) ? "Areas." + areaName + "." : String.Empty;

    var controllerTypeTemplate = "ContosoUniversity.{1}Features.{0}.UiController";
    var controllerType = String.Format(controllerTypeTemplate, controllerName, areaNamespace);

    return typeof(ControllerFactory).Assembly.GetType(controllerType);
}

No change was needed for the Area Registration class, which was a pleasant surprise.