[英]Why is WebAPI action found as GET but 404 when I use POST?
I have an action on my controller called GetPhotos. 我在控制器上有一个名为GetPhotos的操作。 When I call the action as a GET with the appropriate parms, the action is called and everything works great.
当我使用适当的参数将操作作为GET调用时,将调用该操作,并且一切正常。 (I'm using attribute routing)
(我正在使用属性路由)
GET Code 获取代码
[Route("getphotos")]
public IHttpActionResult GetPhotos(string userid, string password, string system, string outstation, string systemno, string version, string compid, string photono)
{
//Do some logic here
return OK();
}
GET call GET电话
GET http://localhost:60672/getphotos?userid=User1++&password=12345&system=UserSystem&outstation=9&systemno=3&version=6.78&compid=mycomputername&photono=003573
HTTP/1.1
User-Agent: Fiddler
Host: localhost:60672
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
However, when I change the action to HttpPost and change my call to a POST call (via Fiddler), I get back a 404. 但是,当我将操作更改为HttpPost并将调用更改为POST调用(通过Fiddler)时,我得到了404。
POST Code 邮递区号
[HttpPost]
[Route("getphotos")]
public IHttpActionResult GetPhotos(string userid, string password, string system, string outstation, string systemno, string version, string compid, string photono)
{
//Do some logic here
return OK();
}
POST Call 开机自检
POST http://localhost:60672/getphotos HTTP/1.1
Content-Type: application/x-www-form-urlencoded
User-Agent: Fiddler
Host: localhost:60672
Content-Length: 170
Pragma: no-cache
USERID=User1&PASSWORD=12345&SYSTEM=UserSystem&OUTSTATION=9&SYSTEMNO=3&VERSION=6%2E79&COMPID=mycomputername&PHOTONO=003573
Why am I unable to find the action with a POST? 为什么我无法通过POST找到操作?
I found the answer here . 我在这里找到了答案。
Apparently, I had two things wrong. 显然,我有两件事做错了。 I'm used to MVC and was trying to do things the MVC way rather than the WebAPI way.
我已经习惯了MVC,并试图以MVC方式而不是WebAPI方式进行操作。
First, I needed the [FromBody] attribute in front of my parameters since that is where I wanted the model binding to pull from. 首先,我需要在参数前面添加[FromBody]属性,因为这是我要从中提取模型绑定的地方。
However, it also appears that WebAPI will only allow one [FromBody] parameter for any given action. 但是,对于任何给定的操作,WebAPI似乎也只允许一个[FromBody]参数。 So, what I wound up doing was creating a Model that encapsulated all my values and changed my method definition to look like this:
因此,我最后要做的是创建一个模型,该模型封装了我的所有值并更改了方法定义,如下所示:
public IHttpActionResult GetPhotos([FromBody] GetPhotoModel model) { ... }
Hope this saves you from some of the same frustration! 希望这可以使您摆脱同样的挫败感!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.