ASP .Net Core Http [GET],用于收集嵌套对象 [英] ASP .Net Core Http [GET] for collection of nested objects
本文介绍了ASP .Net Core Http [GET],用于收集嵌套对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在查询字符串中使用嵌套对象的集合 喜欢
I want to use a collection of nested objects in a query string like
public class Range
{
public decimal From {get;set},
public decimal To {get;set}
}
public class SearchParams
{
public IEnumerable<Range> Sizes {get;set;}
}
我知道最好为此使用POST,但是我不能更改现有合同 由于向后兼容
I know that better to use POST for this but I can't change an existing contract because of backward compatibility
因此,问题-是否有任何选项可以在查询字符串中传递它?或将其作为字符串传递并在服务器中解析
So the question - Is there any option to pass it in a query string? or pass it as a string and to parse it in server
推荐答案
假设您的操作方法收到一个SearchParams
作为参数,名为para
.
Suppose you action method receives a SearchParams
as parameter , which is named as para
.
public IActionResult Index(SearchParams para)
{
return new JsonResult(para);
}
您可以按以下方式发送GET
请求:
you can send the GET
request as below :
GET https://localhost:44386/?para.sizes[0].from=1.1¶.sizes[0].To=1.2¶.sizes[1].from=2.1¶.sizes[1].To=2.2& HTTP/1.1
查询字符串:
para.sizes[0].from=1.1¶.sizes[0].To=1.2¶.sizes[1].from=2.1¶.sizes[1].To=2.2
,响应将是:
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Kestrel
X-SourceFiles: =?UTF-8?B?RDpccmVwb3J0XDIwMThcMTBcMTFcU08uR2V0Q29sbGVjdGlvblxBcHA=?=
{
"sizes": [{
"from": 1.1,
"to": 1.2
}, {
"from": 2.1,
"to": 2.2
}]
}
这篇关于ASP .Net Core Http [GET],用于收集嵌套对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文