Asp .NET Core 2.2 Razor 页面布局和局部
Posted
技术标签:
【中文标题】Asp .NET Core 2.2 Razor 页面布局和局部【英文标题】:Asp .NET Core 2.2 Razor Pages Layour and Partials 【发布时间】:2019-09-21 22:11:46 【问题描述】:我正在使用 Razor Pages 在 Asp .NET Core 2.2 中构建应用程序。我的登录页面已设置,我想重定向到主页。
public IActionResult OnGet()
try
if (User.Identity.IsAuthenticated)
return RedirectToPage("/Account/Index");
catch (Exception ex)
Console.Write(ex);
return Page();
public async Task<IActionResult> OnPostAsync()
try
// Verification.
if (ModelState.IsValid)
// Dominio LDAP
string domain = "sd6.glb.corp.local";
// Verificação de autenticação na AD.
var isValid = LdapAuthenticate(domain, login.Username, login.Password);
if (isValid)
var user = GetUser();
// Login.
await SignInUser(user, login.RememberMe);
// Info.
return RedirectToPage("/Account/Index");
else
// Setting.
ModelState.AddModelError(string.Empty, "Username / Password Incorrecto(s).");
catch (Exception ex)
Console.Write(ex);
return Page();
问题是当我被重定向到帐户/索引页面时
我得到了这个异常
InvalidOperationException:传入的模型项 ViewDataDictionary 是类型 'Abastecimento.Pages.Account.IndexModel',但这个 ViewDataDictionary 实例需要类型的模型项 'Abastecimento.Pages.Shared.Pages_Shared__NavbarPartial'。
这是我的布局
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - Abastecimento</title>
<environment include="Development">
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="~/lib/adminlte/dist/css/adminlte.css" />
</environment>
<environment exclude="Development">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute"
crossorigin="anonymous"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"/>
</environment>
<link rel="stylesheet" href="~/css/site.css" />
</head>
<body class="sidebar-mini">
<div class="wrapper">
<!-- Navbar -->
<partial name="_NavbarPartial" />
<!-- /.navbar -->
<partial name="_SidebarPartial" />
<!-- Main Sidebar Container -->
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">
<!-- Content Header (Page header) -->
<div class="content-header">
<div class="container-fluid">
@*<div class="row mb-2">
<div class="col-sm-6">
<h1 class="m-0 text-dark">Starter Page</h1>
</div><!-- /.col -->
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="#">Home</a></li>
<li class="breadcrumb-item active">Starter Page</li>
</ol>
</div><!-- /.col -->
</div><!-- /.row -->*@
</div><!-- /.container-fluid -->
</div>
<!-- /.content-header -->
<!-- Main content -->
<div class="content fade-in">
<div class="container-fluid my-5 pb-4">
@RenderBody()
</div><!-- /.container-fluid -->
</div>
<!-- /.content -->
</div>
<!-- /.content-wrapper -->
<!-- Main Footer -->
<partial name="_FooterPartial" />
</div>
<environment include="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
<script src="~/lib/adminlte/dist/js/adminlte.js"></script>
</environment>
<environment exclude="Development">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
asp-fallback-test="window.jQuery"
crossorigin="anonymous"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=">
</script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.bundle.min.js"
asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"
asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
crossorigin="anonymous"
integrity="sha384-xrRywqdh3PHs8keKZN+8zzc5TX0GRTLCcmivcbNJWm2rs5C8PRhcEn3czEjhAO9o">
</script>
</environment>
<script src="~/js/site.js" asp-append-version="true"></script>
@RenderSection("Scripts", required: false)
</body>
</html>
这是我的部分导航栏
@page
<!-- Navbar -->
<nav class="main-header navbar navbar-expand navbar-dark bg-danger border-bottom custom-sidebar fixed-top">
<!-- Left navbar links -->
<ul class="navbar-nav">
<li class="nav-item ">
<a class="nav-link custom-nav-link custom-navbar-links" data-widget="pushmenu" href="#"><i class="fas fa-bars"></i></a>
</li>
<li class="nav-item d-none d-sm-inline-block">
<a href="/Account/Index" class="nav-link custom-navbar-links">
<p>
<i class="fas fa-home"></i>
Início
</p>
</a>
</li>
<li class="nav-item d-none d-sm-inline-block">
<a href="/Account/Home/Suporte" class="nav-link custom-navbar-links">
<p>
<i class="fas fa-question"></i>
Suporte
</p>
</a>
</li>
</ul>
<!-- Right navbar links -->
<ul class="navbar-nav ml-auto">
<li class="nav-item">
<a class="nav-link custom-navbar-links" href="/Account/Logout">
<i class="fas fa-sign-out-alt"></i>
Terminar Sessão
</a>
</li>
</ul>
</nav>
<!-- /.navbar -->
这在使用局部视图的 asp net MVC 5 上可以正常工作,但我无法弄清楚是什么导致了这个错误。
【问题讨论】:
【参考方案1】:通常您应该从登录页面重定向到主页/索引。 如果您发送索引页面,我可以告诉您更好的理由。
【讨论】:
以上是关于Asp .NET Core 2.2 Razor 页面布局和局部的主要内容,如果未能解决你的问题,请参考以下文章
Asp .Net Core 2.2 Razor Pages Ajax Call Post 不工作
如何在asp.net core razor pages 2.2中设置日期格式和文化