[英]How do I define the Route for a .Net Core 3.1 webapi service call which takes an array of strings as a parameter?
I'm create a.NetCore 3.1 Web API Service.我正在创建一个.NetCore 3.1 Web API 服务。
I am having difficulties figuring out the correct routing definition for the service call.我很难确定服务调用的正确路由定义。
Consider the following definition:考虑以下定义:
[HttpPut]
[Route("{paramA}/{paramB}")]
public void Sample(string paramA, string[] paramB)
{
}
What is the correct way to define my routing parameters when passing either an array of strings?传递字符串数组时定义路由参数的正确方法是什么? Should I be using List instead?我应该改用 List 吗?
I suggest that you could pass the array list by query string:我建议您可以通过查询字符串传递数组列表:
[HttpPut]
[Route("{paramA}")]
public void Sample([FromRoute]string paramA, [FromQuery]string[] paramB)
{
}
Send Request like: https://localhost:5001/xxxxxx/aaa?paramB=ss¶mB=sd
.发送请求,例如: https://localhost:5001/xxxxxx/aaa?paramB=ss¶mB=sd
。
I recomend you to pass the Arraay strings on the body of the request in a HttpPost, this is the part of the request where you don't have character limitations, when in the url of the query you will have something like 300 character limits.我建议您在 HttpPost 的请求正文中传递 Array 字符串,这是您没有字符限制的请求部分,当在查询的 url 中您将有类似 300 个字符的限制。
For example:例如:
HttpPost]
[Route("MyArrayStrings")]
public async string MyArrayStrings([FromBody] string content)
{
return content;
}
And then, you can use Newtonsoft for serialize the data when sending the request, and for Deserialize when received.然后,您可以在发送请求时使用 Newtonsoft 对数据进行序列化,并在收到时进行反序列化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.