ASP.NET Core MVC form submission returns HTTP 405 Method Not Allowed when using attribute routing

15 hours ago 3
ARTICLE AD BOX

I’m building an ASP.NET Core MVC app using Razor views and Bootstrap modals for Login and Register forms.
When submitting the forms, I consistently get

HTTP 405 – Method Not Allowed.

Is it because the razor page has two forms embedded? I have tried changing the route attributes but it was still not working, could the two forms inside a single page be the issue and would making them show up in different razor pages fix the issue?

Controller

using Chance_Profit.Models; using Microsoft.AspNetCore.Mvc; namespace Chance_Profit.Controllers { public class HomeController : Controller // no ApiController { [HttpGet] public IActionResult Index() { ViewData["Title"] = "Chance Profit - Home"; return View(); } [HttpPost("login")] [ValidateAntiForgeryToken] public IActionResult Login(IndexViewModel model) { Console.WriteLine("Login action invoked"); if (!ModelState.IsValid) { ViewData["Title"] = "Chance Profit - Home"; return View("Index", model); } // TODO: Perform login logic return RedirectToAction(nameof(Dashboard)); } [HttpPost("register")] [ValidateAntiForgeryToken] public IActionResult Register(IndexViewModel model) { if (!ModelState.IsValid) { ViewData["Title"] = "Chance Profit - Home"; return View("Index", model); } // TODO: Perform registration logic return RedirectToAction(nameof(Dashboard)); } [HttpGet] public IActionResult Dashboard() { ViewData["Title"] = "Chance Profit - Dashboard"; return View(); } } }

Razor Page

@model Chance_Profit.Models.IndexViewModel @{ ViewData["Title"] = ViewData["Title"] ?? "Chance Profit"; } <!DOCTYPE html> <html> <head><title>Chance Profit</title> <link rel="stylesheet" href="~/lib/bootstrap/css/bootstrap.min.css" /> <script src="~/lib/bootstrap/js/bootstrap.bundle.min.js"></script> <script src="~/js/customjs.js"></script> </head> <body> <div class="text-center"> <h1 class="display-4">@ViewData["Title"]</h1> <button type="button" class="btn btn-primary m-2" data-bs-toggle="modal" data-bs-target="#loginModal"> Login </button> <button type="button" class="btn btn-success m-2" data-bs-toggle="modal" data-bs-target="#registerModal"> Register </button> </div> <!-- Login Modal --> <div class="modal fade" id="loginModal" tabindex="-1" aria-labelledby="loginModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="loginModalLabel">Login</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <form asp-action="Login" asp-controller="Home" method="post" id="login" > <div class="mb-3"> <label asp-for="LoginInput.Email" class="form-label"></label> <input asp-for="LoginInput.Email" class="form-control" placeholder="Enter email" name="LoginInput.Email"> <span class="text-danger" asp-validation-for="LoginInput.Email"></span> </div> <div class="mb-3"> <label asp-for="LoginInput.Password" class="form-label"></label> <input asp-for="LoginInput.Password" class="form-control" placeholder="Password" type="password"> <span class="text-danger" asp-validation-for="LoginInput.Password"></span> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="submit" class="btn btn-primary" onclick="handleLogin()">Login</button> </div> @Html.AntiForgeryToken() </form> </div> </div> </div> </div> <!-- Register Modal --> <div class="modal fade" id="registerModal" tabindex="-1" aria-labelledby="registerModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="registerModalLabel">Register</h5> <button type="button" class="btn-close" data-bs-dismiss="modal"></button> </div> <div class="modal-body"> <form asp-action="Register" method="post" id="register"> <div class="mb-3"> <label asp-for="RegisterInput.FullName" class="form-label"></label> <input asp-for="RegisterInput.FullName" class="form-control" placeholder="Full Name"> <span class="text-danger" asp-validation-for="RegisterInput.FullName"></span> </div> <div class="mb-3"> <label asp-for="RegisterInput.Email" class="form-label"></label> <input asp-for="RegisterInput.Email" class="form-control" placeholder="Email"> <span class="text-danger" asp-validation-for="RegisterInput.Email"></span> </div> <div class="mb-3"> <label asp-for="RegisterInput.Password" class="form-label"></label> <input asp-for="RegisterInput.Password" class="form-control" placeholder="Password" type="password"> <span class="text-danger" asp-validation-for="RegisterInput.Password"></span> </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button> <button type="submit" class="btn btn-success">Register</button> </div> @Html.AntiForgeryToken() </form> </div> </div> </div> </div> </body> </html> @section Scripts { <partial name="_ValidationScriptsPartial" /> }

TylerH's user avatar

TylerH

21.3k86 gold badges84 silver badges122 bronze badges

So Few Against So Many's user avatar

2

Read Entire Article