.net core not routing to POST method

11,090

Solution 1

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

Solution 2

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".

Solution 3

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

Share:
11,090

Related videos on Youtube

crumdev
Author by

crumdev

Experience creating multi-stage build containers for .Net Core using Linux containers. Experience configuring GitLab CI/CD to build apps on commit and deploy to Netlify. Experience implementing interdependent containers using docker-compose.yml configuration (VPN Container in front of Torrent container with NGINX reverse proxy container for accessing the torrent container behind the VPN). Automated payroll process for businesses incorporated through acquisition through API extraction, transformation, and loading into corporate payroll systems. Experience in writing database queries, stored procedures, materialized views for query optimizations, using Common Table Expressions (CTEs). Proficient in analyzing, designing, and implementing web and process applications. Ability to clearly develop and document a product from start to finish. Utilize Dependency Injection and configuration over convention for greater flexibility in application usage. Experienced in Agile and Waterfall development environments.

Updated on June 28, 2022

Comments

  • crumdev
    crumdev almost 2 years

    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

  • crumdev
    crumdev over 6 years
    Thank you! That fixed the problem. If I had a separate ContactController would I have needed to explicitly have the routes above the methods for this still?