http获取 - 带有身体和URI Parms- [来自Body]变量为null

发布于 2025-02-03 15:33:29 字数 1148 浏览 1 评论 0原文

我有一个已作为帖子实现的端点。它实际上是基础的,实际上可能是 /应该是一个问题,但是由于所需的数据包括集合或列表,所以我将其更改为带有JSON主体的帖子。

但是团队发现这令人困惑。因此,现在我被要求用URI参数和人体列表作为JSON来实现。

因此,从本质上讲,我需要做的就是这样的事情:

 GET http://localhost:123/locations/{locationID}/products/{productID}/stats/{dateRange}
 
 Body:  {"count":["exports","imports","anothertiem", "thisListCanbeLong"]}

我试图更改C#功能签名以使其看起来像这样:

    [HttpGet]
    [Route("/locations/{locationID}/products/{productID}/stats/{dateRange}")]
    [AllowAnonymous] 
    public ProductStats GetStats(ProductStatRequest request, [FromBody] List<string> typesofStats)
    {

然后,当我尝试使用这种类型的请求使用人才或邮递员来调用端点时:

 http://localhost:123/locations/4/products/all/stats/15

headers:headers:

 Content-Type header is set to "application/json"

主体

{"count":["exports","imports","anothertiem", "thisListCanbeLong"]}

通话失败了TypeOfStats列表为null。不确定如何在这种类型的端点中提取身体。

另外,虽然我确实读到可以处理身体,但这也许不是最好的方法。也许帖子更好,我只需要回到团队中说不,或者帖子可以同时处理Uri Parms和身体。 团队的问题是,他们希望在URI中看到通过的位置ID和产品ID等 - 使其与其余的呼叫保持一致。

感谢您的帮助。

I have an endpoint that has been implemented as a POST. It's actually idemponent and really could be / should be a GET but because the data required includes a collection or a list, I changed it to a POST with a json body.

But the team finds this confusing. So now I'm being asked to implement as a GET with both URI parameters and a list in the body as JSON.

So essentially what I need to do is something like this:

 GET http://localhost:123/locations/{locationID}/products/{productID}/stats/{dateRange}
 
 Body:  {"count":["exports","imports","anothertiem", "thisListCanbeLong"]}

I've tried to change my c# function signature to look like this:

    [HttpGet]
    [Route("/locations/{locationID}/products/{productID}/stats/{dateRange}")]
    [AllowAnonymous] 
    public ProductStats GetStats(ProductStatRequest request, [FromBody] List<string> typesofStats)
    {

And then when I try to call the endpoint using either Talent or Postman using this type of a request:

 http://localhost:123/locations/4/products/all/stats/15

Headers:

 Content-Type header is set to "application/json"

Body

{"count":["exports","imports","anothertiem", "thisListCanbeLong"]}

the call fails the typeofStats list is null. Not sure how I can extract the body in this type of an endpoint.

Also, while I did read that GET can handle body, maybe this isn't the best approach. Maybe the POST is better and I just have to get back to the team and say no, or maybe the POST can handle both URI parms and body.
The issue for the team is that they want to see in the uri the location id being passed and the product id etc - to keep it uniform with the rest of the calls.

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2025-02-10 15:33:30

ASP.NET MVC框架可与命名约定一起使用,这意味着参数名称必须匹配请求数据名称。随着以下更改应起作用:

[AllowAnonymous, HttpGet("/locations/{locationID}/products/{productID}/stats/{dateRange}")]
public ProductStats GetStats(
    [FromRoute] ProductStatRequest request, 
    [FromBody] List<string> typesofStats)
{
    return new ProductStats
    { 
        Request = request,
        TypesOfStats = typesofStats
    };
}

public class ProductStatRequest
{
    public int LocationId { get; set; }

    public string ProductId { get; set; }

    public int DateRange { get; set; }
}

public class ProductStats
{
    public ProductStatRequest Request { get; set; }

    public List<string> TypesOfStats { get; set; }
}

...以及Postman的请求/响应

“在此处输入图像说明”

如果您想在请求正文中提供一个列表中的对象,则应期望相同的对象在动作方法中。

{"count":["exports","imports","anothertiem", "thisListCanbeLong"]}

应该期待:

public class StatsTypes
{
    public List<string> Count { get; set; }
}

The Asp.net MVC framework works with naming conventions, which means that the parameter names must match the request data names. With the following changes should work:

[AllowAnonymous, HttpGet("/locations/{locationID}/products/{productID}/stats/{dateRange}")]
public ProductStats GetStats(
    [FromRoute] ProductStatRequest request, 
    [FromBody] List<string> typesofStats)
{
    return new ProductStats
    { 
        Request = request,
        TypesOfStats = typesofStats
    };
}

public class ProductStatRequest
{
    public int LocationId { get; set; }

    public string ProductId { get; set; }

    public int DateRange { get; set; }
}

public class ProductStats
{
    public ProductStatRequest Request { get; set; }

    public List<string> TypesOfStats { get; set; }
}

... and the request/response from postman

enter image description here

If you want to provide an object with a list in it in the body of the request, you should expect the same object in the action method.

{"count":["exports","imports","anothertiem", "thisListCanbeLong"]}

should be expected as:

public class StatsTypes
{
    public List<string> Count { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文