How do I resolve a HTTP 405 error in my login?

Amra Akmadzic 100 Reputation points
2024-08-20T15:53:48.7966667+00:00

My asp.net core MVC web application uses Identity for authentication and authorization, but my login doesn't work. After entering data and pressing the button in login I get a HTTP 405 error and message "This page isn't working".

My view uses POST method inside <form> tag, as well as my controller.

Here is my Account Controller:


public class AccountController : Controller

{

    private readonly VjencanjeIzSnovaDbContext _context;

    private readonly UserManager<Korisnik> _userManager;

    private readonly SignInManager<Korisnik> _signInManager;

    public AccountController(UserManager<Korisnik> userManager, SignInManager<Korisnik> signInManager, VjencanjeIzSnovaDbContext context)

    {

        _userManager = userManager;

        _signInManager = signInManager;

        _context = context;

    }

    [HttpGet]

    [Route("login")]

    public IActionResult Login()

    {

        return View(new LoginViewModel());

    }

    [HttpPost]

    public async Task<IActionResult> Login(LoginViewModel model)

    {

        if (ModelState.IsValid)

        {

            var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);

            if (result.Succeeded)

            {

                return RedirectToAction("Index", "Home");

            }

            ModelState.AddModelError(string.Empty, "Neuspješan pokušaj prijave.");

        }

        return View(model);

    }

}


@model VjencanjeIzSnova_WebApp.ViewModels.LoginViewModel
<title>Prijava</title>
<div class="container d-flex justify-content-center align-items-center min-vh-100">
    <div class="row border rounded-5 p-3 bg-white shadow box-area">
        <div class="col-md-6 rounded-4 d-flex justify-content-center align-items-center flex-column left-box" style="background: #b05059;">
            <div class="featured-image mb-3">
                <i class="bi bi-box-arrow-in-right"></i>
            </div>
            <p class="text-white fs-2" style="font-family: 'Courier New', Courier, monospace; font-weight: 600;">Prijavite se</p>
            <small class="text-white text-wrap text-center" style="width: 17rem;font-family: 'Courier New', Courier, monospace;">Organizacija vjenčanja nikad nije bila lakša.</small>
        </div>
        <div class="col-md-6 right-box">
            <form asp-action="Login" method="post">
                <div class="row align-items-center">
                    <div class="header-text mb-4">
                        <h2>Dobrodošli!</h2>
                        <p>Drago nam je vidjeti Vas opet.</p>
                    </div>
                    <div class="input-group mb-3">
                        <input asp-for="Email" type="email" class="form-control form-control-lg bg-light fs-6" placeholder="Email adresa" />
                        <span asp-validation-for="Email" class="text-danger"></span>
                    </div>
                    <div class="input-group mb-1">
                        <input asp-for="Password" type="password" class="form-control form-control-lg bg-light fs-6" placeholder="Lozinka" />
                        <span asp-validation-for="Password" class="text-danger"></span>
                    </div>
                    <div class="input-group mb-5 d-flex justify-content-between">
                        <div class="form-check">
                            <input asp-for="RememberMe" type="checkbox" class="form-check-input" id="formCheck">
                            <label for="formCheck" class="form-check-label text-secondary"><small>Remember Me</small></label>
                        </div>
                        <div class="forgot">
                            <small><a href="#">Zaboravili ste lozinku?</a></small>
                        </div>
                    </div>
                    <div class="input-group mb-3">
                        <button type="submit" class="btn btn-lg btn-primary w-100 fs-6" style="background: #c0737a">Login</button>
                    </div>
                    <div class="row">
                        <small>Nemate korisnički račun? <a asp-controller="Account" asp-action="Registracija">Registrirajte se</a></small>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>


.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,919 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,612 questions
0 comments No comments
{count} votes

Accepted answer
  1. youzeliang 735 Reputation points
    2024-08-20T16:04:59.1533333+00:00

    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)
    
    
    1. Check the Form Method and Action: Ensure that the form's method="post" is correct and matches the HttpPost method in your controller.
    2. 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 (or Program.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

    2 people found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.