skip to Main Content

I’m encountering an issue in my ASP.NET Core application where I’m receiving an HTTP 405 error when attempting to submit a form to a controller action. I’ve reviewed my code and configuration but haven’t been able to identify the root cause of the problem. Here are the details:

I have a form in my ASP.NET Core application that is meant to handle user login. The form is configured to use the POST method, and it is supposed to submit the user’s email and password to a controller action named "login."

HTML Form:

@{
    ViewData["Title"] = "Sign In";
}

<h2>Sign In</h2>

<form asp-action="login" asp-controller="Home" method="post">
    <div class="form-group">
        <label>Email</label>
        <input name="email" class="form-control" />
    </div>
    <div class="form-group">
        <label>Password</label>
        <input name="password" type="password" class="form-control" />
    </div>
    <button type="submit" class="btn btn-primary">Sign In</button>
</form>

Controller Action:

    [HttpPost]
        public IActionResult login(string email, string password)
        {
            var user = _context.Users.SingleOrDefault(u => u.email == email && u.password == password);
            if (user != null)
            {
                return RedirectToAction("Index");
            }
            ModelState.AddModelError(string.Empty, "Invalid login attempt.");
            return View();
        }

    }
}

Program.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Routing.Constraints;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using signIn_form.Models;

var builder = WebApplication.CreateBuilder(args);



builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddControllersWithViews();

var app = builder.Build();


// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();

When I submit the form, I receive an HTTP 405 error with the message "This page isn’t working. If the problem continues, contact the site owner. HTTP ERROR 405." I have verified that the "login" action is correctly decorated with the [HttpPost] attribute to handle POST requests.The HTML form specifies the correct method="post".There are no apparent issues with the controller and action names. ASP.NET Core version: 7.0.3

I would greatly appreciate any insights or suggestions on how to resolve this issue. If you need more code or configuration details, please let me know, and I’ll provide them promptly.

Thank you for your help!

2

Answers


  1. It looks like you haven’t set up a route for the login endpoint? According to the documentation, you should either set up a conventional route in the Program.cs file, or use attributes on your controller (which you may have done, but you did not include the entire code, so I won’t know). 405 indicates that the server receives the request, but cannot map it to a function.

    Login or Signup to reply.
  2. <form asp-action="login" asp-controller="Home" method="post">

    Please use browser F12 developer tools to inspect the html source code and check if above your Form Tag Helper with asp-action and asp-controller attributes is rendered to be action="/Home/login" as expected.

    Besides, for testing purpose, you can try to use HTML form instead of ASP.NET Core Form Tag Helper, like below. And check if the request can be posted to target endpoint.

    <form method="post" action="/Home/login">
        <div class="form-group">
            <label>Email</label>
            <input name="email" class="form-control" />
        </div>
        <div class="form-group">
            <label>Password</label>
            <input name="password" type="password" class="form-control" />
        </div>
        <button type="submit" class="btn btn-primary">Sign In</button>
    </form>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search