14

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • did you considered this question http://stackoverflow.com/questions/165779/are-the-put-delete-head-etc-methods-available-in-most-web-browsers – FosterZ Nov 18 '11 at 12:51
  • Good post @FosterZ, thanks. However, I'm looking for the workaround implementation in ASP.NET MVC. Do you know how can I implement that? – Saeed Neamati Nov 18 '11 at 13:02

3 Answers3

27

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)
}

Adam Tuliper
  • 29,982
  • 4
  • 53
  • 71
7

HTML forms (up to HTML version 4 and XHTML 1) only support GET and POST as HTTP request methods. XHTML 2.0 will support GET, POST, PUT and DELETE for forms.

A workaround for this for methods through POST by using a hidden form field which is read by the server and dispatch accordingly.

For now, you may consider using [HttpPost] now or use XmlHttpRequest to use Put verb on your request.


UPDATE

You may use SimplyRestfulRouteHandler from MvcContrib

It quite simple, register this on RegisterRoutes

public static void RegisterRoutes(RouteCollection routes)
{
     SimplyRestfulRouteHandler.BuildRoutes(routes);
}

Add a hidden field like this with name _method inside your form

<input type="hidden" name="_method" value="put" />

This would be good to go.

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
  • But I know that ASP.NET supports HTTP Put out of the box. How can I make ASP.NET MVC set a hidden field in my form and dispatch the submitted (posted) form to my proper method, which has `[HttpPut]` attribute? – Saeed Neamati Nov 18 '11 at 13:02
  • 1
    ASP.Net supports it out of the box, yes, but it's the *browser* that you need to pay attention to. Just because your server-side code recognizes the PUT verb doesn't mean the client does. Browsers submitting HTML forms only support GET and POST. – EAMann Nov 18 '11 at 22:01
4

You could use $.ajax to submit the form with the correct verb

Here is an example

suing
  • 2,808
  • 2
  • 16
  • 18