C# IViewLocationExpander不适用于局部视图(MVC核心)

C# IViewLocationExpander不适用于局部视图(MVC核心),c#,asp.net-core,model-view-controller,C#,Asp.net Core,Model View Controller,我已经定义了一个自定义的IViewLocationExpander来检查多租户web应用程序中的站点特定视图 假设有两个租户WebsiteA和WebsiteB以及HomeController和Index视图的以下文件结构 观点 家 网站 Index.cshtml Index.cshtml 我的IViewLocationExpander将为网站A呈现视图/Home/WebSiteA/Index.cshtml,为网站B呈现视图/Home/Index.cshtml——因为没有特定于网站

我已经定义了一个自定义的IViewLocationExpander来检查多租户web应用程序中的站点特定视图

假设有两个租户WebsiteA和WebsiteB以及HomeControllerIndex视图的以下文件结构

  • 观点
      • 网站
        • Index.cshtml
      • Index.cshtml
我的IViewLocationExpander将为网站A呈现视图/Home/WebSiteA/Index.cshtml,为网站B呈现视图/Home/Index.cshtml——因为没有特定于网站B的索引视图,所以它使用默认视图

我还在视图中设置了一个名为“Common”的文件夹来保存任何局部视图,其思想是我可以以相同的方式渲染自定义局部视图(例如标题)

  • 观点
    • 普通的
      • 网站
        • _Header.cshtml
      • _Header.cshtml
这是我的IViewLocationExpander的代码

public sealed class TenantViewLocationExpander : IViewLocationExpander
{
    private ITenantService _tenantService;
    private string _tenant;

    public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
    {
        string[] locations =
        {
            "/Views/{1}/" + _tenant + "/{0}.cshtml",
            "/Views/Common/" + _tenant + "/{0}.cshtml",
            "/Views/Shared/" + _tenant + "/{0}.cshtml",
            "/Pages/Shared/" + _tenant + "/{0}.cshtml",
            "/Views/{1}/{0}.cshtml",
            "/Views/Common/{0}.cshtml",
            "/Views/Shared/{0}.cshtml",
            "/Pages/Shared/{0}.cshtml"
        };
        return locations;
    }

    public void PopulateValues(ViewLocationExpanderContext context)
    {
        _tenantService = context.ActionContext.HttpContext.RequestServices.GetRequiredService<ITenantService>();
        _tenant = _tenantService.GetCurrentTenant();
    }
}
似乎扩展器没有为局部视图添加额外的位置。所以我的问题是,如何配置IViewLocationExpander来处理Partials

在旧版本的MVC中,我看到您可以通过设置ViewLocationFormatsPartialViewLocationFormats来专门定义它们,但在MVC Core中,我在任何地方都看不到该选项

抱歉,如果这已经被其他地方掩盖了-我在任何地方都找不到答案


提前谢谢

对于任何阅读本文的人来说,他们可能有同样的问题-似乎需要从部分标记中省略“.cshtml”

<!--- Works with all ViewLocations defined in IViewLocationExpander --->
<partial name="_Header" />

然而,如果您包含如下所示的“.cshtml”,它似乎只搜索默认的局部视图位置-“视图/共享”

<!--- Only looks in 'Views/Shared' --->
<partial name="_Header.cshtml" />


不确定这是否是故意的,但在我看来,这绝对是MVC中的一个奇怪错误。

谢谢!我以为我疯了。对我来说,这绝对是个问题。
<!--- Works with all ViewLocations defined in IViewLocationExpander --->
<partial name="_Header" />
<!--- Only looks in 'Views/Shared' --->
<partial name="_Header.cshtml" />