将对象列表作为查询参数发送到ASP.NET Web控制器

huangapple go评论57阅读模式
英文:

Send ASP.NET web controller a list of objects in the query params

问题

The issue might be with the URL encoding of the discriminatorTags parameter. It appears to be encoded as JSON, which might not be parsed correctly by your ASP.NET controller. It should be formatted differently.

You may want to change the way you send the discriminatorTags parameter in your URL request. Instead of sending it as a JSON object, you can send it as a list of integers directly in the URL, like this:

http://localhost:5002/AllVgnItmEsts/searchall?currentpage=1&pagesize=6&hasEst=true&discriminatorTags=1

This assumes that you want to pass a list of integers for the tags property of the DiscriminatorTags struct. Adjust the URL format to match your specific requirements.

英文:

Is it clear why discriminatorTags query param is coming into my asp.net web controller as an empty list when I'm trying to send it as a list with one item?

[HttpGet("searchall")]
[EnableCors("MyPolicy")]
public ActionResult<IEnumerable<VgnItmEstSearchDto>> SearchAll(
    [FromQuery(Name = "searchterm")] string searchTerm = "",
    [FromQuery(Name = "currentpage")] int pageNumber = 1,
    [FromQuery(Name = "pagesize")] int pageSize = 6,
    [FromQuery(Name = "hasEst")] bool hasEst = true,
    [FromQuery(Name = "discriminatorTags")] List<DiscriminatorTags> discriminatorTags = null
    )
{
    var results = service.SearchAll(searchTerm, pageNumber, pageSize, hasEst, discriminatorTags);
    return Ok(results);
}

It's sending this url request (sorry it won't let me copy paste it) so I guess the issue is in here (how should discriminatorTags url encoding here be formatted differently?):

将对象列表作为查询参数发送到ASP.NET Web控制器

This is the C# stuct I want to send a list of:

public struct DiscriminatorTags : IDiscriminatorTags
{
    public DiscriminatorTags(List<int> tags, string discriminator, bool selected)
    {
        this.Tags = tags;
        this.Discriminator = discriminator;
        this.Selected = selected;
    }

    [Required]
    public List<int> Tags { get; set; }
    [Required]
    public string Discriminator { get; set; }
    [Required]
    public bool Selected { get; set; }
}

I ran swagger (I couldn't get any swagger api requests to work, it said there is either a CORS issue or network failure) and the generated url for this web request is:

http://localhost:5002/AllVgnItmEsts/searchall?currentpage=1&pagesize=6&hasEst=true&discriminatorTags=%7B%0A%20%20%22tags%22%3A%20%5B%0A%20%20%20%201%0A%20%20%5D%2C%0A%20%20%22discriminator%22%3A%20%22RestaurantItm%22%2C%0A%20%20%22selected%22%3A%20true%0A%7D

And when I paste the "discriminatorTags" portion of that into my api request url, discriminatorTags is still coming into my web controller as an empty list.

答案1

得分: 2

在ASP.NET中,直接从查询参数绑定对象列表是不可能的。但是,您可以通过使用自定义模型绑定器或手动解析和绑定数据来实现类似的功能。

英文:

In ASP.NET, it is not directly possible to bind a list of objects from a query parameter out of the box. However, you can achieve similar functionality by using a custom model binder or by manually parsing and binding the data.

huangapple
  • 本文由 发表于 2023年5月17日 15:06:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269366.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定