为什么在ASP.NET MVC中调用没有属性的函数,即使请求是post请求?(Why is a function without attribute is being called in ASP.NE

系统教程 行业动态 更新时间:2024-06-14 17:01:34
为什么在ASP.NET MVC中调用没有属性的函数,即使请求是post请求?(Why is a function without attribute is being called in ASP.NET MVC even though the request is a post request? [closed])

我正在学习本教程:

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.

更多推荐

本文发布于:2023-04-20 18:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/56480259855eb3c2bab162bc82da7f20.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:求是   函数   属性   MVC   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!