HTTP错误400通常表示客户端请求的语法有问题,服务器无法理解。这种错误通常与表单验证或请求参数有关。在ASP.NET Core MVC中,如果您看到“帐户登录给出HTTP错误400:此页面无法正常工作”错误,可能是由于以下几个原因:
@Html.ValidationMessageFor方法来显示验证错误消息。示例代码:
[HttpPost]
public async Task Login(LoginViewModel model)
{
if (ModelState.IsValid)
{
// 处理登录逻辑
// ...
return RedirectToAction("Index", "Home");
}
return View(model);
}
LoginViewModel对象作为参数。示例代码:
[HttpPost]
public async Task Login(LoginViewModel model)
{
// 处理登录逻辑
// ...
}
示例代码:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
Startup.cs文件的ConfigureServices方法中,添加AddAuthentication和AddCookie方法来配置Cookie身份验证。示例代码:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
});
// ...
}
这些是可能导致ASP.NET Core MVC显示“帐户登录给出HTTP错误400:此页面无法正常工作”的一些常见问题和解决方法。请根据您的具体情况逐个排查这些问题,并根据需要进行调整。