跨区域 ViewComponents

Cross area ViewComponents

我有一个 ViewComponent 存储在名为 "Dashboard" 的区域中,但现在我想在另一个名为 "Appplications" 的区域中使用此 ViewComponent。是的,我可以将它添加到根 views/shared 文件夹,但我正在努力通过对区域的强大包含使用来制作一个非常模块化的应用程序。

ASP.NET 5 RC1 MVC 6 似乎不支持对其他组件的跨区域引用。

如何添加其他视图位置?我需要添加: /Areas/Dashboard/Views/Shared/Components/DashboardMenu/Default.cshtml 作为视图渲染器的搜索位置

InvalidOperationException: The view 'Components/DashboardMenu/Default' was not found. The following locations were searched:
/Areas/Applications/Views/Application/Components/DashboardMenu/Default.cshtml
/Areas/Applications/Views/Shared/Components/DashboardMenu/Default.cshtml
/Views/Shared/Components/DashboardMenu/Default.cshtml.

解决了...

Startup.cs

// Add additional razor view engine configuration to facilitate:
// 1. Cross area view path searches
services.Configure<RazorViewEngineOptions>(options =>
{
    options.ViewLocationExpanders.Add(new RazorViewLocationExpander());
});

然后创建一个名为 RazorViewLocationExpander.cs

的 class
using Microsoft.AspNet.Mvc.Razor;
using System.Collections.Generic;
using System.Linq;

public class RazorViewLocationExpander : IViewLocationExpander
{
    public void PopulateValues(ViewLocationExpanderContext context) { }

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        List<string> locations = viewLocations.ToList();

        locations.Add("/Areas/dashboard/Views/Shared/{0}.cshtml");

        return locations;
    }
}

我一般不会推荐这个。我将此解决方案用作特殊情况,因为我正在使用一个区域来隔离模板和核心代码,供我的其他(仅限会员)区域使用 - 因此他们需要知道在哪里可以找到此共享代码。我试图将 public 代码与管理代码分开,这是我能想到的最干净、最模块化的解决方案。仪表板区域将出现在所有需要仅限会员的管理区域的网站上。它稍微改变了 MVC 的规则。