ASP.NET Core 3.0 - Identity UI 管理未接收布局的文件夹

ASP.NET Core 3.0 - Identity UI Manage folder not receiving layout

我根据文档搭建了 Identity 脚手架,除 /Manage 文件夹的布局外,一切正常。 目录设置与脚手架完全相同。

Directory setup with all misc. files removed

为清楚起见: /Areas/Identity/Pages/Account/Manage 是有问题的文件夹。

/Pages 包含从我的 Views/Shared 文件夹设置布局的 _ViewStart 文件。
/Pages/Account 从 _Viewstart 接收布局并正常工作。
/Pages/Account/Manage 这里的所有内容 只有 接收 _ViewStart 布局。此处的 _Layout 文件不会被其中的页面自动找到。

Areas/Identity/Pages/Account/Manage/_Layout.cshtml

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>Manage your account</h1>

<div>
    <h4>Change your account settings</h4>
    <hr />
    <div class="row">
        <div class="col-md-3">
            <partial name="_ManageNav" />
        </div>
        <div class="col-md-9">
            @RenderBody()
        </div>
    </div>
</div>

@section Scripts {
    @RenderSection("Scripts", required: false)
}

这与它的脚手架完全一样,只有当您将 AddDefaultIdentity() 更改为 AddIdentity() 时布局才会中断。我使用我搭建的参考,它只是让我相信我在删除默认 UI 时没有考虑到某些事情。我发现的唯一解决方法是在 /Manage

中手动设置每个 .cshtml 文件的布局
@{
    Layout = "_Layout";
}

这修复了所有问题并使 /Manage 内页面的布局正常工作。我阅读了文档,它指出每个 Razor 页面控制器在搜索其他地方之前应该在自己的文件夹中搜索 _Layout 文件。它没有检测到文件是有原因的吗?

  1. "The only workaround I found was manually setting the layout of each .cshtml file within /Manage":

    您不必那样做。只需在 Manage/ foler 下创建一个 _ViewStart.cshtml

    @* file: Manage/_ViewStart.cshtml *@
    @{
        Layout = "_Layout";    // Use a partial layout name instead of absolute name
    }
    

    还要注意默认的 Manage/Layout.cshtml 使用 /Areas/Identity/Pages/_Layout.cshtml 的父布局,这可能不存在于您的脚手架文件中:

    @* file: Manage/Layout.cshtml *@
    @{
        Layout = "/Areas/Identity/Pages/_Layout.cshtml";  // you might want to change this to `/Views/Shared/_Layout.cshtml`
    }
    
  2. "it states that each Razor Page controller should search its own folder for a _Layout file before searching elsewhere"

    仅当您使用 部分 _Layout 名称时才如此。 但是,如果您使用以斜杠开头的绝对名称,它将直接使用该布局。见 official docs :

    When a partial name is provided, the Razor view engine searches for the layout file using its standard discovery process. The folder where the handler method (or controller) exists is searched first, followed by the Shared folder. This discovery process is identical to the process used to discover partial views.

    在您的例子中,以 / 开头的布局名称 /Areas/Identity/Pages/_Layout.cshtml 不是部分名称。这就是您的页面无法发现布局的原因。为了解决此问题,请改用部分名称 _Layout。 (这可以像我上面那样通过单个 _ViewStart.cshtml 文件来完成,不要为每个页面添加它)

  3. 最后,你可能想知道为什么使用AddDefaultIdentity()时渲染正常。正如您所发现的,AddDefaultIdentity() 将添加 default UI, which eventually invokes the AddRelatedParts() method。这允许它在没有这样的布局或页面时回退到默认 UI。例如,当您使用 Visual Studio 脚手架 Identity 时,它会提供一个列表,您可以使用该列表来覆盖默认页面。上面的/Areas/Identity/Pages/_Layout.cshtml来自默认的UI。