发送POST请求时,框架将自动调用POST操作.例如,如果你有一个HTML表单:
<% using (Html.BeginForm("Index", "Home", FormMethod.Post)) {%>
<input type="submit" value="OK" />
<% } %>
Run Code Online (Sandbox Code Playgroud)
它会自动调用POST索引操作:
[HttpPost]
public ActionResult Index()
{
...
}
Run Code Online (Sandbox Code Playgroud)
或者您可以使用jquery发送AJAX请求并指定要POST:
$.post('/home/index', function(result) {
alert('successfully invoked the POST index action');
});
Run Code Online (Sandbox Code Playgroud)
就像PUT和DELETE这样的其他动词而言,只有AJAX调用才支持它们.您无法在HTML表单中指定它.虽然有一个解决方法.以下表格:
<% using (Html.BeginForm("Destroy", "Home", FormMethod.Post)) {%>
<%= Html.HttpMethodOverride(HttpVerbs.Delete) %>
<input type="submit" value="OK" />
<% } %>
Run Code Online (Sandbox Code Playgroud)
将调用此操作:
[HttpDelete]
public ActionResult Destroy() {}
Run Code Online (Sandbox Code Playgroud)
这种方式的工作方式是使用POST动词但是另外一个隐藏字段与请求一起发送,允许引擎路由到正确的控制器动作.如果你使用AJAX,那么你可以直接指定你想要的动词:
$.ajax({
url: '/home/destroy',
type: 'DELETE',
success: function(result) {
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
457 次 |
| 最近记录: |