英文:
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?):
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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论