Home > Software design >  httpPost return Page not found HTTP ERROR 405 ASP.NET CORE MVC IIS
httpPost return Page not found HTTP ERROR 405 ASP.NET CORE MVC IIS

Time:06-19

i have problem with form when i put in my Controller httpPost the Error http 405 it showing to me

every step i made its correct

my code

<div >
    <form id="contact" asp-action="Contact" method="POST">
        <h3>Colorlib Contact Form</h3>
        <h4>Contact us for custom quote</h4>
        <fieldset>
            <input placeholder="Your name" asp-for="Name" type="text" tabindex="1" required autofocus>
        </fieldset>
        <fieldset>
            <input placeholder="Your Email Address" asp-for="Email" type="email" tabindex="2" required>
        </fieldset>
        <fieldset>
            <input placeholder="Your Phone Number (optional)" asp-for="PhoneNumber" type="tel" tabindex="3" required>
        </fieldset>

</form>
</div>

and my Controller

namespace security.Controllers
{

    public class HomeController : Controller

    {

        DBContext db;
        public  HomeController(DBContext context)
        {
            db = context;
        }

 public IActionResult Index()
        {

            CollectionsData model = new CollectionsData();
            model.Offers = GetOffers();
            model.Services = GetServices();
            model.News = GetNews();
            model.Team = GetSecurityTeam();

            return View(model);
        }




 [HttpPost]
        public IActionResult Contact(ContactUs model)
        {
            db.Contactus.Add(model);
            db.SaveChanges();
            return RedirectToAction("Index");

            
        
        }
}

But when I delete httppost everything works fine except sending the form to the database

i don't why the page its not load its give HTTP ERROR 405

thanks

CodePudding user response:

try this

<form id="contact"  method="POST" asp-action="Contact"> 
  ...     
</form>

CodePudding user response:

return View() doesn’t make any sense from an HTTP Verb perspective. Your form is POSTing correctly, but when you’re returning the view you need to change it to redirect to your GET action. I don’t know know what your entire controller looks like but I suspect this is the issue with naming.

Try return RedirectToAction(“VIEWNAME”)

  • Related