ASP.NET Core razor 视图未在浏览器中显示
ASP.NET Core razor view doesn't show up in browser
这是浏览器显示给我的:
这是我的 _Layout
:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer _localizer
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>@_localizer[(string)ViewData["Title"]] - Accounter</title>
<link rel="apple-touch-icon" sizes="180x180" href="icons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="icons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="icons/favicon-16x16.png">
<link rel="manifest" href="icons/site.webmanifest">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"/>
<link rel="stylesheet" href="~/css/site.css"/>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2022.1.412/styles/kendo.bootstrap-v4.min.css"/>
<script src="https://kendo.cdn.telerik.com/2022.1.412/js/kendo.all.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2022.1.412/js/kendo.aspnetmvc.min.js"></script>
</head>
<body>
<header>
<nav class="navbar navbar-expand navbar-light">
<div class="container">
<a class="navbar-brand" asp-action="Index">Tasker</a>
<div class="navbar-nav">
@if (User.Identity.IsAuthenticated)
{
<a class="nav-link text-dark" asp-controller="Authentication" asp-action="LogOut">@_localizer["Log out"]</a>
}
else
{
<a class="nav-link text-dark" asp-controller="Authentication" asp-action="Index">@_localizer["Log in"]</a>
}
</div>
<partial name="_SelectLanguagePartial"/>
</div>
</nav>
</header>
<div></div>
<div class="vh-100 pt-5 backdrop">
<div class="container rounded-3 p-3">
<main role="main">
@RenderBody()
</main>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/cdc2f29cb5.js" crossorigin="anonymous"></script>
@await RenderSectionAsync("Scripts", false)
</body>
</html>
我要求用户进行身份验证并重定向到 ConfigureServices
中的身份验证页面。
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Authentication";
options.SlidingExpiration = true;
});
AuthenticationController
:
public class AuthenticationController : Controller
{
private readonly IAuthenticationBL _authenticationBL;
public AuthenticationController(IAuthenticationBL authenticationBL)
{
_authenticationBL = authenticationBL;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register([Bind("Email", "Password")] AuthenticationVM vm)
{
if (!ModelState.IsValid) return View("Index", vm);
await _authenticationBL.RegisterAsync(vm.Email, vm.Password, Request);
return RedirectToAction("Index", "GeneralInformation");
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> LogIn([Bind("Email", "Password")] AuthenticationVM vm)
{
if (!ModelState.IsValid) return View("Index", vm);
await _authenticationBL.LogInAsync(vm.Email, vm.Password, User);
return RedirectToAction("Index", "GeneralInformation");
}
public async Task<IActionResult> LogOut()
{
await _authenticationBL.LogoutAsync(User);
return RedirectToAction("Index");
}
}
身份验证索引视图:
@model Web.Models.VMs.AuthenticationVM
@inject IViewLocalizer _localizer
@using Microsoft.AspNetCore.Mvc.Localization
@{
ViewData["Title"] = "Authentication";
}
<div class="card w-50 container justify-content-center">
<div class="card-body">
<h5 class="text-center mb-3">@_localizer["Log in"]</h5>
<form asp-controller="Authentication" id="authentication-form">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-floating mb-4 row">
<input asp-for="Email" class="form-control" placeholder="Email" autocomplete="email">
<label asp-for="Email">@_localizer["Email"]</label>
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-floating mb-4 row">
<input asp-for="Password" class="form-control" placeholder="Password">
<label asp-for="Password">@_localizer["Password"]</label>
<i class="btn fa-solid fa-eye" id="password-toggle"></i>
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="row justify-content-between">
<button type="submit" class="col-sm-5 btn btn-primary" asp-action="Register">@_localizer["Register"]</button>
<button type="submit" class="col-sm-5 btn btn-primary" asp-action="LogIn">@_localizer["Log in"]</button>
</div>
</form>
</div>
</div>
@section Scripts {
<script src="~/js/authentication.js"></script>
}
问题是我在Startup
中删除了默认操作的指示:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action}");
});
框架无法理解在重定向到控制器时调用什么操作,我没有获得页面。修正:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}");
});
这是浏览器显示给我的:
这是我的 _Layout
:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer _localizer
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>@_localizer[(string)ViewData["Title"]] - Accounter</title>
<link rel="apple-touch-icon" sizes="180x180" href="icons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="icons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="icons/favicon-16x16.png">
<link rel="manifest" href="icons/site.webmanifest">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"/>
<link rel="stylesheet" href="~/css/site.css"/>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2022.1.412/styles/kendo.bootstrap-v4.min.css"/>
<script src="https://kendo.cdn.telerik.com/2022.1.412/js/kendo.all.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2022.1.412/js/kendo.aspnetmvc.min.js"></script>
</head>
<body>
<header>
<nav class="navbar navbar-expand navbar-light">
<div class="container">
<a class="navbar-brand" asp-action="Index">Tasker</a>
<div class="navbar-nav">
@if (User.Identity.IsAuthenticated)
{
<a class="nav-link text-dark" asp-controller="Authentication" asp-action="LogOut">@_localizer["Log out"]</a>
}
else
{
<a class="nav-link text-dark" asp-controller="Authentication" asp-action="Index">@_localizer["Log in"]</a>
}
</div>
<partial name="_SelectLanguagePartial"/>
</div>
</nav>
</header>
<div></div>
<div class="vh-100 pt-5 backdrop">
<div class="container rounded-3 p-3">
<main role="main">
@RenderBody()
</main>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/cdc2f29cb5.js" crossorigin="anonymous"></script>
@await RenderSectionAsync("Scripts", false)
</body>
</html>
我要求用户进行身份验证并重定向到 ConfigureServices
中的身份验证页面。
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Authentication";
options.SlidingExpiration = true;
});
AuthenticationController
:
public class AuthenticationController : Controller
{
private readonly IAuthenticationBL _authenticationBL;
public AuthenticationController(IAuthenticationBL authenticationBL)
{
_authenticationBL = authenticationBL;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register([Bind("Email", "Password")] AuthenticationVM vm)
{
if (!ModelState.IsValid) return View("Index", vm);
await _authenticationBL.RegisterAsync(vm.Email, vm.Password, Request);
return RedirectToAction("Index", "GeneralInformation");
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> LogIn([Bind("Email", "Password")] AuthenticationVM vm)
{
if (!ModelState.IsValid) return View("Index", vm);
await _authenticationBL.LogInAsync(vm.Email, vm.Password, User);
return RedirectToAction("Index", "GeneralInformation");
}
public async Task<IActionResult> LogOut()
{
await _authenticationBL.LogoutAsync(User);
return RedirectToAction("Index");
}
}
身份验证索引视图:
@model Web.Models.VMs.AuthenticationVM
@inject IViewLocalizer _localizer
@using Microsoft.AspNetCore.Mvc.Localization
@{
ViewData["Title"] = "Authentication";
}
<div class="card w-50 container justify-content-center">
<div class="card-body">
<h5 class="text-center mb-3">@_localizer["Log in"]</h5>
<form asp-controller="Authentication" id="authentication-form">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-floating mb-4 row">
<input asp-for="Email" class="form-control" placeholder="Email" autocomplete="email">
<label asp-for="Email">@_localizer["Email"]</label>
<span asp-validation-for="Email" class="text-danger"></span>
</div>
<div class="form-floating mb-4 row">
<input asp-for="Password" class="form-control" placeholder="Password">
<label asp-for="Password">@_localizer["Password"]</label>
<i class="btn fa-solid fa-eye" id="password-toggle"></i>
<span asp-validation-for="Password" class="text-danger"></span>
</div>
<div class="row justify-content-between">
<button type="submit" class="col-sm-5 btn btn-primary" asp-action="Register">@_localizer["Register"]</button>
<button type="submit" class="col-sm-5 btn btn-primary" asp-action="LogIn">@_localizer["Log in"]</button>
</div>
</form>
</div>
</div>
@section Scripts {
<script src="~/js/authentication.js"></script>
}
问题是我在Startup
中删除了默认操作的指示:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action}");
});
框架无法理解在重定向到控制器时调用什么操作,我没有获得页面。修正:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}");
});