[英]asp-controller and asp-action not working in ASP.NET Core 3.1 Razor Pages app
I have an ASP.NET Core 3.1 Razor Pages app which allows users to log in and out.我有一个 ASP.NET Core 3.1 Razor Pages 应用程序,它允许用户登录和注销。
To log out, I have the following Controller:要注销,我有以下 Controller:
public sealed class AccountController : Controller
{
[Route("/Logout")]
public async Task<IActionResult> LogoutAsync()
{
await this.HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
return this.RedirectToPage(ApplicationConstants.IndexPage);
}
}
In _Layout.cshtml
file I have the following:在_Layout.cshtml
文件中,我有以下内容:
<a class="nav-link" asp-controller="Account" asp-action="LogoutAsync"><i class="fa fa-sign-out fa-lg" aria-hidden="true"></i></a>
In _ViewImports.cshtml
file, which is in the parent directory of _Layout.cshtml
, I have the following:在_ViewImports.cshtml
的父目录中的_Layout.cshtml
文件中,我有以下内容:
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
In Startup.cs
file, I added the following:在Startup.cs
文件中,我添加了以下内容:
public void ConfigureServices(IServiceCollection services)
{
// stuff
services.AddControllers();
services.AddRazorPages()
.AddRazorPagesOptions(options =>
{
options.Conventions.AuthorizePage("/Page1");
options.Conventions.AuthorizePage("/Page2");
});
// more stuff
}
public static void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// stuff
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
Visual Studio seems to be happy with this setup and recognises asp-controller
and asp-action
as ASP.NET Core Tag Helpers. Visual Studio 似乎对此设置感到满意,并将asp-controller
和asp-action
识别为 ASP.NET 核心标记帮助程序。
However, when I run the application, the link is rendered as the following (notice the empty href
):但是,当我运行应用程序时,链接呈现如下(注意空的href
):
<a class="nav-link" href=""><i class="fa fa-sign-out fa-lg" aria-hidden="true"></i></a>
What am I missing?我错过了什么?
Notes:笔记:
As far as I know, if you want to let the a tag generate the herf, you should set the route template in the Configure method.据我所知,如果想让a标签生成herf,应该在Configure方法中设置路由模板。
More details, you could refer to below codes:更多细节,您可以参考以下代码:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
Result:结果:
Looks like it is a known and intentional behaviour that was introduced in ASP.NET Core 3.0.看起来这是 ASP.NET Core 3.0 中引入的已知且有意的行为。
If the Action Method has Async
suffix, it will be ignored;如果 Action Method 有Async
后缀,则会被忽略; hence, asp-action
will not see LogoutAsync
as a valid Action.因此, asp-action
不会将LogoutAsync
视为有效的 Action。
For reference, see this documentation about migrating to ASP.NET Core 3.0 from 2.2 and this GitHub issue.有关参考,请参阅有关从 2.2 迁移到 ASP.NET Core 3.0 的文档以及此GitHub问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.