HTML表单上的ASP.NET MVC中的PUT或DELETE谓词

Sae*_*ati 13 forms rest asp.net-mvc http-put asp.net-mvc-3

我有一个简单的用户注册表单,有两个字段,一个用于用户名,另一个用于密码.我有一个控制器UserController,它有这两个动作:

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

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

我使用HTTP Put来使我的网站RESTful(插入PUT动词).但是,当我提交表单时,我收到404错误.这是我的表单的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>
Run Code Online (Sandbox Code Playgroud)

我在这里想念什么?怎么了?

Ada*_*SFT 24

由于这里没有人真的回答,我会加上我的.02

MVC将以下字段添加到html表单(例如,在使用Html.BeginForm()时)以支持此字段.尽管Html5实际上并不支持HttpPut和HttpDelete(它们只是在某一点然后从草案规范中删除),但是当发布以下表单字段时,MVC的服务器库将正确地将请求路由到您的HttpDelete或HttpPut方法.您的HttpPost请求:


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


Abd*_*nim 7

HTML表单(最高为HTML版本4和XHTML 1)仅支持GET和POST作为HTTP请求方法.XHTML 2.0将支持表单的GET,POST,PUT和DELETE.

通过使用由服务器读取并相应调度的隐藏表单字段通过POST的方法的解决方法.

目前,您可以考虑立即使用[HttpPost]XmlHttpRequest使用Put动词来处理您的请求.


UPDATE

您可以使用SimplyRestfulRouteHandlerMvcContrib

很简单,注册这个 RegisterRoutes

public static void RegisterRoutes(RouteCollection routes)
{
     SimplyRestfulRouteHandler.BuildRoutes(routes);
}
Run Code Online (Sandbox Code Playgroud)

添加一个hidden字段这样有名字_method里面你form

<input type="hidden" name="_method" value="put" />
Run Code Online (Sandbox Code Playgroud)

这样会很好.