I'm so happy to solove it
in my opinion,
Check the asp-action
attribute in the form tag: Ensure that the asp-action="Login"
correctly maps to the Login
action method in your AccountController
. Since you didn't specify the controller in the asp-action
, ensure that the view is using the AccountController
.
If your view is in a different controller, specify the controller name explicitly:
<form asp-controller="Account" asp-action="Login" method="post">
Verify the Route Attribute in Your Controller: You have a [Route("login")]
attribute on the HttpGet
Login
action. Ensure there's no routing conflict that might prevent the POST request from being handled correctly.
To avoid any issues, you could explicitly set the route for the POST method:
[HttpPost]
[Route("login")]
public async Task<IActionResult> Login(LoginViewModel model)
Alternatively, remove the [Route("login")]
from both actions and rely on the default route:
[HttpGet]
public IActionResult Login()
{
return View(new LoginViewModel());
}
[HttpPost]
public async Task<IActionResult> Login(LoginViewModel model)
- Check the Form Method and Action: Ensure that the form's
method="post"
is correct and matches theHttpPost
method in your controller. - Middleware Configuration: Ensure that your application is correctly configured to use routing and that the middleware pipeline includes endpoints. This is typically done in the
Startup.cs
(orProgram.cs
in .NET 6+) file:
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}"); });
Test with Explicit Routes: If the problem persists, try temporarily specifying both the action and controller in the form:
<form asp-controller="Account" asp-action="Login" method="post">
i hope you can accept my answer