PUT或HTML表单在ASP.NET MVC DELETE动词 [英] PUT or DELETE verb in ASP.NET MVC on HTML form

查看:216
本文介绍了PUT或HTML表单在ASP.NET MVC DELETE动词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的用户登记表,具有两个字段,一个用于用户名和另一个输入密码。我有一个名为 UserController的控制器,它有两个动作:

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

我用的 HTTP PUT ,以使我的网站的RESTful(PUT动词插入)。然而,当我提交我的形式,我得到404错误。这里是我的表单的HTML:

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?

推荐答案

既然没有人真的在这里回答了这个问题,我会加上我0.02

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

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

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

这篇关于PUT或HTML表单在ASP.NET MVC DELETE动词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆