ardalis / OrganizingAspNetCore

Offers several different ways to organize content in ASP.NET Core MVC and Razor Pages projects.
http://ardalis.com
MIT License
209 stars 60 forks source link

Controller in one feature returns view from separate feature #6

Open ardalis opened 7 years ago

ardalis commented 7 years ago

The current version has two features:

Each has a SwordsController with an Index.cshtml view inside of a Swords subfolder.

Going to /ninjas/swords hits the SwordsController in /Features/Ninjas and returns the expected View. Going to /samurai/swords hits the SwordsController in /Features/Samurai but returns the view from /Features/Ninjas/Swords/index.cshtml.

dotnetshadow commented 7 years ago

Any progress on this? I think I alerted you with a similar issue. I sort of have a work around (not the best I know) by adding an area route to each of the controllers. I've modified my view locations to handle that too.

public static void ConfigureFeatureFoldersCustom(this RazorViewEngineOptions options)
        {
            // {0} - Action Name
            // {1} - Controller Name
            // {2} - Area Name
            // {3} - Feature Name

            // add support for features side-by-side with /Views
            // (do NOT clear ViewLocationFormats)
            options.ViewLocationFormats.Insert(0, "/Features/Shared/{0}.cshtml");
            options.ViewLocationFormats.Insert(0, "/Features/{3}/{0}.cshtml");
            options.ViewLocationFormats.Insert(0, "/Features/{3}/{1}/{0}.cshtml");
            options.ViewLocationFormats.Insert(0, "/Features/{2}/Shared/{0}.cshtml");
            options.ViewLocationFormats.Insert(0, "/Features/{2}/{3}/{1}/{0}.cshtml");

            // (do NOT clear AreaViewLocationFormats)
            options.AreaViewLocationFormats.Insert(0, "/Areas/{2}/Features/Shared/{0}.cshtml");
            options.AreaViewLocationFormats.Insert(0, "/Areas/{2}/Features/{3}/{0}.cshtml");
            options.AreaViewLocationFormats.Insert(0, "/Areas/{2}/Features/{3}/{1}/{0}.cshtml");

            // Areas within features
            options.AreaViewLocationFormats.Insert(0, "/Features/{3}/{1}/{0}.cshtml");

            options.ViewLocationExpanders.Add(new FeatureViewLocationExpander());
        }

FooController.cs

[Area("Admin")]
    [Route("[area]/[controller]/")]
    public class FooController : Controller

/features/admin/foo

dotnetshadow commented 7 years ago

Not sure if this is associated with your issue but I found this: https://github.com/OdeToCode/AddFeatureFolders/pull/17