.net core not routing to POST method
The POST action needs to have a route as well if the intention is to use attribute routing.
[HttpGet]
[Route("contact")]
public IActionResult Contact() {
return View();
}
[HttpPost]
[Route("contact")]
public IActionResult Contact(string name, string email, string message) {
ViewBag.Name = name;
ViewBag.Email = email;
ViewBag.Message = message;
return View();
}
Note the exclusion of the slashes as they are not needed. Make sure the names and ids of the form inputs match the parameters of the target action
It looks like you're missing the Route
attribute on the [HttpPost]
method.
Try this.
[HttpPost]
[Route("contact/")]
public IActionResult Contact(string name, string email, string message)
Also update your view code, so that the name
property of your <input />
tags matches the arguments of your controller action.
Remember that MVC uses the name property to bind to the arguments in your controller action. MSDN Model Binding
For example update your email input to include the name property:
<input name="email" id="email" class="input" type="text" placeholder="Email" value="@ViewBag.Email">
You'll also need to update the text area name to name="message"
.