WebApi HTTPPOST端点未命中 [英] WebApi HTTPPOST Endpoint not being hit

查看:67
本文介绍了WebApi HTTPPOST端点未命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下简单的HTTPPOST端点;

I have the following simple HTTPPOST endpoint;

[AllowAnonymous]
[HttpPost]
[Route("forgotPassword")]
public IHttpActionResult ForgotPassword(string userName, string callbackUrl)

控制器的装饰如下:

[Authorize]
[RoutePrefix("api/accounts")]
public class AccountsController : ApiController

现在,当我尝试使用以下网址在邮递员中测试此终结点时;

Now when i try to test this endpoint in postman, using the following url;

http://localhost:11217/api/accounts/forgotPassword

在邮件正文中带有字符串

with the strings in the body of the message

我得到以下回报.

{"Message":未找到与请求匹配的HTTP资源URI' http://localhost:11217/api/accounts/forgotPassword '.",
"MessageDetail":在控制器'Accounts'上未找到任何操作符合要求."}

{ "Message": "No HTTP resource was found that matches the request URI 'http://localhost:11217/api/accounts/forgotPassword'.",
"MessageDetail": "No action was found on the controller 'Accounts' that matches the request." }

现在,我宁愿不必为两个字符串创建模型(如果可能的话).另外,如果我尝试将参数放入查询字符串中,则会收到潜在的危险请求响应

Now I would rather not have to create a model for the two strings (if possible). Also if I try to put the params in the query string I get a potantially dangerous request response

http://localhost:11217/api/accounts/forgotPassword/test&callbackUrl = local

任何人都可以帮忙吗?

推荐答案

如果您要在执行发布请求时发送多个参数,则应创建一个包含以下参数的DTO

If you want to send mulitple parameters when doing a post request you should create a DTO that contains the parameters as

public class forgetPasswordDTO
{
    public string userName { get; set; }
    public string callbackUrl { get; set; }
}

然后使用[FromBody]将DTO添加为方法参数

Then add the DTO as a method parameter with the [FromBody]

[AllowAnonymous]
[HttpPost]
[Route("forgotPassword")]
public IHttpActionResult ForgotPassword([FromBody] forgetPasswordDTO data)

然后在您的客户端中,将对象创建为

And in you client, create the object as

var data = {
    'userName': user,
    'callbackUrl': url
};

并将其添加到请求的正文中.

And add it to the body of the request.

Here's a nice article about this topic

这篇关于WebApi HTTPPOST端点未命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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