我正在学习本教程:
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/examining-the-edit-methods-and-edit-view
它描述了当用户单击按钮然后调用没有属性的函数时的情况。
但是当添加具有HttpPost属性的函数时,将调用此函数,而不是没有该属性的函数。
该按钮启动Html.BeginForm(),它创建一个post请求。 那么为什么没有调用属性的函数被调用呢?
I'm following this tutorial:
http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/examining-the-edit-methods-and-edit-view
It describes a situation when a user click a button and then a function without an attribute is called.
But when a function with HttpPost attribute is added, this function is called and not the function without the attribute.
The button initiates Html.BeginForm() which creates a post request. So why did the function without the attribute got called?
最满意答案
提交按钮将始终发起HttpPost请求。
另一方面(标签)的超链接指向一个动作将始终是一个HttpGet 。
编辑:我意识到你的困惑,请看下面的内容,以证明@RickAndMSFT试图说:
鉴于我们有以下行动方法:
[HttpGet] public ActionResult SearchIndex() { }此操作仅接受HttpGet请求
注意:如果我们取消HttpGet属性,它将接受所有请求类型 。
因此,如果你有:
public ActionResult SearchIndex() { }不需要post方法,因为上面的方法已经接受了post请求。 情况就是这样,因为有问题的页面实际上并没有修改/保存/创建任何内容,它只是列出数据。
A submit button will ALWAYS initiate a HttpPost request.
A hyperlink on the other hand (a tag), that points to an action will ALWAYS be a HttpGet.
EDIT: I realise your confusion, see below to demonstrate what @RickAndMSFT is trying to say:
Given that we have the following action method:
[HttpGet] public ActionResult SearchIndex() { }This action will ONLY accept HttpGet requests
NOTE: If we take away the HttpGet attribute, it will accept ALL Request Types.
Therefore, if you have:
public ActionResult SearchIndex() { }There is no need for a post method because the method above already accepts post requests. This is the case because the page in question isn't actually modifying/saving/creating anything, it's just listing data.
更多推荐
发布评论