ASP.NET Core 是一个开源的、跨平台的框架,用于构建现代的、基于云的、互联网连接的应用程序。在 ASP.NET Core 中,布局(Layout)是一种用于定义应用程序用户界面一致性的机制。通过使用布局,可以创建一个共享的页面模板,然后在多个视图中重用这个模板。
ASP.NET Core 中的布局主要有以下几种类型:
布局通常用于以下场景:
问题描述:在 ASP.NET Core 3.0 中,布局不适用于用户配置文件页面。
可能原因:
Startup.cs
中正确配置了布局。解决方法:
Startup.cs
中正确配置了布局文件路径。例如:Startup.cs
中正确配置了布局文件路径。例如:Startup.cs
中配置授权策略:Startup.cs
中配置授权策略:以下是一个简单的示例,展示如何在 ASP.NET Core 3.0 中配置布局:
Startup.cs:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
Views/Shared/_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - My ASP.NET Core App</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" asp-area="" asp-page="/Index">My ASP.NET Core App</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/UserProfile/Index">Profile</a>
</li>
</ul>
</div>
</div>
</header>
<div class="container">
<main role="main" class="pb-3">
@RenderBody()
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2023 - My ASP.NET Core App - <a asp-area="" asp-page="/Privacy">Privacy</a>
</div>
</footer>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="~/js/site.js" asp-append-version="true"></script>
</body>
</html>
Views/UserProfile/Index.cshtml:
@{
ViewData["Title"] = "Profile";
}
<h1>User Profile</h1>
<p>This is the user profile page.</p>
领取专属 10元无门槛券
手把手带您无忧上云