【问题标题】:Layout not working on asp.net core 3.0 for user profile用户配置文件的布局在 asp.net core 3.0 上不起作用
【发布时间】:2019-10-31 22:27:34
【问题描述】:

在 Asp core 3.0 上,当用户单击其个人资料“Areas/Identity/Pages/Account/Manage/_layout.cshtml”时,我的布局页面无法正常工作

当我在登录后单击默认电子邮件时,我得到 Manage/Index.cshtml 但没有布局详细信息。像导航一样,忘记密码等的链接没有显示。我不知道是什么原因造成的。

我将它与其他项目对比,发现有任何差异,但找不到我的布局不显示的任何原因。

区域/身份/页面/帐户/管理/_layout.cshtml

@{
Layout = "/Areas/Identity/Pages/_Layout.cshtml";
 }

区域/身份/页面/帐户/管理/Index.cshtml

@page
@model IndexModel
@{
  ViewData["Title"] = "Profile";
  ViewData["ActivePage"] = ManageNavPages.Index;
}

区域/身份/页面/帐户/_ViewStart.cshtml

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

尝试查看我缺少的内容或任何其他要检查的地方以显示布局页面。

【问题讨论】:

  • 您期望的布局是什么?默认为Manage。一般来说,如果你能登录,你应该知道密码,为什么要显示忘记密码Manage页面?
  • 我想在创建新的 asp.net 核心应用程序时使用默认布局。当您单击您的电子邮件时,它会将您带到您的管理页面。我错过了我的默认布局,带有用于更改密码、编辑个人资料、下载个人数据等的侧面导航。我得到的只是一个带有电子邮件和电话号码的表单,Manage/_Layout.cshtml 没有呈现
  • 分享您当前的结果?是否有任何演示可以重现您的问题?
  • 我附上截图,我想要的布局没有渲染
  • 是否有任何演示可以重现您的问题?对于Profile,它位于_ManageNav.cshtml 中,并与Pages/Account/Manage/_Layout.cshtml 中的<partial name="_ManageNav" /> 一起使用。

标签: asp.net asp.net-mvc asp.net-core


【解决方案1】:

我有同样的问题,我怀疑在您替换 startup.ConfigureServices 中的 services.AddDefaultIdentity 调用时会发生同样的问题 - 在我的情况下,我将其替换为 AddIdentity,因为我需要将 RoleManager 传递给我的控制器。

这个问题很可能有一个更优雅的解决方案,但我放弃了寻找一个,因为我发现这个全新的功能没有很好的文档记录。

这是我解决这个问题的方法:

我假设您已经搭建了身份页面 - 如果没有,请参阅:https://docs.microsoft.com/en-us/aspnet/core/security/authentication/scaffold-identity?view=aspnetcore-3.0&tabs=visual-studio

在页面Areas\Identity\Pages\Account\Manage_Layout.cshtml下修改布局位置为:

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

现在在您的索引页面 Areas\Identity\Pages\Account\Manage\Index.cshtml 中,您还需要显式设置布局页面:

@page
@model IndexModel
@{
    Layout = "_Layout.cshtml";
}
@{
    ViewData["Title"] = "Profile";
    ViewData["ActivePage"] = ManageNavPages.Index;
}

您还必须在该文件夹下的所有其他页面中明确设置此布局。

【讨论】:

  • 下半场似乎不再需要了。只需在 Areas\Identity\Pages\Account\Manage\_Layout.cshtml 中设置 Layout = "/Views/Shared/_Layout.cshtml"; 即可解决我在 Net Core 3.0 Web MVC 项目中的问题(在搭建所有页面后)
  • 不确定有什么区别,但我在一个 3.1 核心 MVC 项目中,默认情况下标识区域脚手架,并且仍然必须在 Index 页面中添加 Layout = 行。这当然是在替换 services.AddDefaultIdentity 之后它在此之前运行良好。
  • 现在是 2021 年,这也解决了 .NET 5 中的确切问题。
  • 值得注意的是,如果您使用的是 blazor,则应将其更改为 /Pages/Shared/_Layout.cshtml..
【解决方案2】:

设置时在启动类中使用AddDefaultUI()

services.AddIdentity<User, Role>().AddDefaultUI() 

【讨论】:

    【解决方案3】:

    更新。

    要解决问题安装Microsoft.AspCoreNet.Identity.UI NuGet 包并添加AddIdentity。

     services.AddIdentity<User, IdentityRole>(options => options.SignIn.RequireConfirmedEmail)
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultTokenProviders()
        .AddDefaultUI();
    

    但这是有代价的。添加 AddDefaultUI() 解决了 ActivePage 的问题,但它创建了另一个如上所述的问题。 AddDefaultUI() 导致我的总体布局消失。然后我应用了上面建议的解决方案并恢复了布局,但 ActivePage 又消失了。

    我没有花费太多时间,而是删除了 Identity 区域中的所有文件并搭建了 Identity。 TaDa,ActivePage 和我的布局都可以完美运行。

    我正在使用 .Net Core 5.0.0

    https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnetcore#identity-adddefaultui-method-overload-removed

    【讨论】:

    • 好吧,与此同时,我意识到我并不完全正确。如果您安装了 nuGet 包 Microsoft.AspCoreNet.Identity.UI(它已更新到版本 5.0.0)并且 .AddDefaultUI 再次可用。它成功了......但是,然后我失去了我的总体布局。我尝试按照描述修复此问题并取回布局,但随后我丢失了 Activepage ...我因此删除了身份区域和脚手架身份 ... tada,一切都很好,除了这里和那里需要一些额外的编码... 顺便提一句。我正在使用 Core 5。
    【解决方案4】:

    就我而言,我只是将 ViewStart.cshtml 放在了错误的目录中。 我应该将此文件放在 Views 文件夹中,但我将 _ViewStart 文件与 _Layout.cshtml 一起放在 Views/Shared 中

    【讨论】:

      【解决方案5】:

      您需要将部分类_ManageNav.cshtml 添加到您的布局中。 就在RenderBody()之前:

          <div class="row">
              <div class="col-3">
                  <partial name="_ManageNav" />
              </div>
              <div class="col-9">
                  <main role="main">
                      @RenderBody()
                  </main>
              </div>
          </div>
      

      【讨论】:

        猜你喜欢
        • 2020-06-21
        • 2021-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多