asp.net-core - ASP.NET Core 3.0 - 身份 UI 管理文件夹未接收布局

标签 asp.net-core asp.net-identity razor-pages

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

Directory setup with all misc. files removed

为清楚起见:/Areas/Identity/Pages/Account/Manage是问题文件夹。
/Pages包含从我的 View /共享文件夹设置布局的 _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 Page Controller 应该在搜索其他地方之前搜索自己的文件夹以查找 _Layout 文件。有没有原因它没有检测到文件?

最佳答案

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



    你不必那样做。只需创建一个 _ViewStart.cshtml在您的 Manage/ 下文件夹:
    @* 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`
    }
    
  • "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 文件来完成,就像我上面所做的那样,不要为每个页面添加它)
  • 最后,你可能想知道为什么它在使用 AddDefaultIdentity() 时可以正常渲染。 .如您所见,AddDefaultIdentity()将添加 default UI ,最终调用 AddRelatedParts() method .这允许它在没有这样的布局或页面时回退到默认 UI。例如,当您搭建 Identity 时与 Visual Studio ,它提供了一个列表,您可以使用它来覆盖默认页面。以上/Areas/Identity/Pages/_Layout.cshtml来自默认用户界面。
  • 关于asp.net-core - ASP.NET Core 3.0 - 身份 UI 管理文件夹未接收布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58667989/

    相关文章:

    asp.net-core - asp.net core - 如何提供没有扩展名的静态文件

    c# - 如何使用 lambda 表达式进行验证以验证某个小时是否为某个 "type"(必须为 hh :00 or hh:30)

    asp.net-mvc-5 - ASP.NET Identity - 什么方法创建表?

    c# - 如何从 Razor Page 获取 Azure 数据表 ETag 值

    asp.net - 如何将图像上传到 Razor 页面中的模型?

    validation - 使用 ASP.NET 5 禁用客户端验证

    asp.net-core - 如何更改 Microsoft 身份验证提供商登录的默认回调?

    asp.net-mvc-5 - Asp.net身份Google帐户ID从openId迁移到oauth

    asp.net-core - 如果值发生变化,如何调用选择列表中的函数或方法?

    c# - Azure Webjob 服务的 Microsoft.Azure.ServiceBus.Message 支持