You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ASP.NET Core Razor页面OnGet/OnPost方法未执行问题求助

问题描述

站点横幅的分部页面中放置了登出按钮,表单代码如下:

<form id="logoutForm" class="form-inline" method="post" asp-area="Identity" asp-page="/Account/SessionLoggedOut">
    <button id="logout" type="submit" class="btnAsLink">Logout</button>
</form>

点击按钮后,SessionLoggedOut页面正常显示,但页面后台的OnGetAsync和OnPostAsync方法均未执行,日志语句也未输出。需求是用户进入该页面时完成登出操作,但目前无法触发任何页面方法。页面后台代码如下:

public async Task<IActionResult> OnGetAsync()
{
    _logger.LogInformation("SessionLoggedOut.OnGet executed");
    await _signInManager.SignOutAsync();
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
    
    return Page();
}

public async Task<IActionResult> OnPostAsync()
{
    _logger.LogInformation("SessionLoggedOut.OnPost executed");
    await _signInManager.SignOutAsync();
    await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);


    return Page();
}
解决方案

1. 确认页面类继承自PageModel

如果后台类没有继承PageModel,Razor Pages不会识别并执行OnGetAsync/OnPostAsync方法,页面只会作为静态HTML返回。检查SessionLoggedOut.cshtml.cs的类定义:

public class SessionLoggedOutModel : PageModel
{
    private readonly ILogger<SessionLoggedOutModel> _logger;
    private readonly SignInManager<IdentityUser> _signInManager;

    public SessionLoggedOutModel(ILogger<SessionLoggedOutModel> logger, SignInManager<IdentityUser> signInManager)
    {
        _logger = logger;
        _signInManager = signInManager;
    }

    // 你的OnGetAsync和OnPostAsync方法...
}

2. 为POST表单添加防伪令牌

ASP.NET Core Razor Pages默认验证POST请求的防伪令牌,缺少令牌会导致请求被拦截(可能静默返回页面但不执行后台方法)。在表单内添加防伪令牌:

<form id="logoutForm" class="form-inline" method="post" asp-area="Identity" asp-page="/Account/SessionLoggedOut">
    @Html.AntiForgeryToken()
    <button id="logout" type="submit" class="btnAsLink">Logout</button>
</form>

或使用Tag Helper方式:

<form id="logoutForm" class="form-inline" method="post" asp-area="Identity" asp-page="/Account/SessionLoggedOut" asp-antiforgery="true">
    <button id="logout" type="submit" class="btnAsLink">Logout</button>
</form>

3. 验证Identity区域路由配置

确保Program.cs(或Startup.cs)中正确配置了Razor Pages和Identity服务:

// Program.cs
var builder = WebApplication.CreateBuilder(args);

// 添加Razor Pages服务
builder.Services.AddRazorPages();
// 添加Identity服务
builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

// 映射Razor Pages路由(包含Identity区域)
app.MapRazorPages();

app.Run();

4. 检查页面文件路径

确认SessionLoggedOut.cshtml和SessionLoggedOut.cshtml.cs位于正确路径:Areas/Identity/Pages/Account/SessionLoggedOut.cshtml,路径错误会导致无法关联后台模型类。

5. 优化登出逻辑(最佳实践)

更推荐利用Identity自带的Logout页面处理登出,完成后跳转到SessionLoggedOut页面:

  • 修改表单提交到Logout页面:
<form id="logoutForm" class="form-inline" method="post" asp-area="Identity" asp-page="/Account/Logout">
    @Html.AntiForgeryToken()
    <input type="hidden" asp-for="ReturnUrl" value="/Identity/Account/SessionLoggedOut" />
    <button id="logout" type="submit" class="btnAsLink">Logout</button>
</form>
  • 在Logout页面的OnPostAsync中完成登出并跳转:
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
    await _signInManager.SignOutAsync();
    _logger.LogInformation("User logged out.");
    return returnUrl != null ? LocalRedirect(returnUrl) : RedirectToPage("/Index");
}

内容的提问来源于stack exchange,提问作者L. Wallach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 09:44:49