Link to project here -> https://github.com/crumdev/hqbbq.git
I have a simple 3 page MVC website with 1 controller. For the contact page I have a small 3 input form. Below are the Controller methods:
[Route("contact/")]
public IActionResult Contact()
{
return View();
}
[HttpPost]
public IActionResult Contact(string name, string email, string message)
{
ViewBag.Name = name;
ViewBag.Email = email;
ViewBag.Message = message;
return View();
}
When I submit the form with a breakpoint inside of the Contact method for HttpPost it never breaks but instead uses the regular method and just returns the view again. I have tried reducing my form to just the name field and only capture that and it does not enter the POST method. I have given the regular Contact method the [HttpGet] attribute so it can only be used strictly for GET requests and when I submit the form then it bypasses my controller altogether and returns the Dev exception page that is blank except for "Hello World!" on the screen. I have read through the documentation and followed tutorials on teamtreehouse.com for regular ASP.Net as well but cannot understand why it is behaving this way.
Edit: Here is the code for the page submitting the POST. I am just using a plain HTML form with the POST method for submitting the data.
https://github.com/crumdev/hqbbq/blob/master/HQ-BBQ/Views/Home/contact.cshtml
Routing in ASP.NET Core. Routing in ASP.NET Core is the process… | by Packt_Pub | Quick Code | Medium Routing in ASP.NET Core is the process of mapping incoming requests to application logic that resides in controllers and methods.
When you create a WEB API with .NET Core framework, you can notice in its Startup.cs file, This declaration of, ‘app.UseMvc ()’ at configure section, enables Attribute Routing. This are by-default configurations of .NET Core applications. Explicit configurations thus are not required for enabling Attribute routing for .NET Core Web APIs.
However, all versions of ASP.NET Core support the same set of route template features and route constraints. The following example shows routing with health checks and authorization:
The name doesn't affect the routing procedure, but it can be very useful when there are route failures, and ASP.NET Core notifies you on issues with the routes. template: This is the core of the route. This defines the URL structure and the tokens that should be mapped to the controller, actions, and parameters.
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"
.
You can use the FromForm annotation for your parameters in the controller post method
[HttpPost]
[Route("contact")]
public IActionResult Contact([FromForm]string name, [FromForm]string email, [FromForm]string message)
I would also recommend to use a viewmodel rather than passing all the fields of your form as parameters. Imagine you have a form with 10 fields, your method signature would be a way harder to read
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With