Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PUT or DELETE verb in ASP.NET MVC on HTML form

I have a simple user registration form, with two fields, one for username and another for the password. I have a controller called UserController which has these two actions:

[HttpGet]
public ActionResult Register()
{
    return View();
}

[HttpPut]
public ActionResult Register(string username, string password)
{
    // Registering user
    return View();
}

I used HTTP Put to make my website RESTful (PUT verb for insertion). However, when I submit my form, I get 404 error. Here is my form's HTML:

<form action='@Url.Action("Register", "User")' method="post">
<div class='field'>
    <label for='username'>
        Username:
    </label>
    <input type='text' id='username' name='username' maxlength='100' />
</div>
<div class='field'>
    <label for='password'>
        Password:
    </label>
    <input type='password' id='password' name='password' maxlength='50' />
</div>
</form>

What do I miss here? What's wrong?

like image 250
Saeed Neamati Avatar asked Nov 18 '11 12:11

Saeed Neamati


1 Answers

Since no one really answered it here, I'll add my .02

MVC adds the following field to the html form (used for example when using Html.BeginForm()) to support this. Even though HttpPut and HttpDelete are not actually supported for Html5 (they were at one point and then removed from the draft specification), the server libs for MVC will properly route the request to your HttpDelete or HttpPut method when the following form field is posted with your HttpPost request:


@using (Html.BeginForm("SomeAction"){ 
   @Html.HttpMethodOverride(HttpVerbs.Delete)
}

like image 140
Adam Tuliper Avatar answered Sep 30 '22 03:09

Adam Tuliper