简体   繁体   English

如何为将字符串数组作为参数的 .Net Core 3.1 webapi 服务调用定义路由?

[英]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&paramB=sd .发送请求,例如: https://localhost:5001/xxxxxx/aaa?paramB=ss&paramB=sd

Result:结果: 在此处输入图像描述

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.

相关问题 如何在 .NET Core 3.1 中调用 WCF web 服务? - How do I call a WCF web service in .NET Core 3.1? 在 C# ASP.NET Core 3.1 应用程序中,我如何路由到 controller 然后查询字符串? - In a C# ASP.NET Core 3.1 application how do I route to a controller then query string? 如何在 ASP.NET Core 3.1 中获取服务的实例 - How do I get a instance of a service in ASP.NET Core 3.1 如何使用 JavaScript 从 .NET Core 3.1 正确调用 HttpPost 方法避免 400 错误 - How do I use JavaScript to properly call a HttpPost method from .NET Core 3.1 avoiding the 400 error 如何在 Net core 3.1 上路由所有子域 - How to route all subdomain on Net core 3.1 如何在 .net 核心 3.1 应用程序中将 JavaScript 阵列发送到 controller? - How do I send a JavaScript array to a controller in a .net core 3.1 application? 如何在 ASP.NET Core 3.1 WebApi 中使用自定义记录器登录一些不同的文件? - How can i log in some different files with custom logger in ASP.NET Core 3.1 WebApi? 如何在 .NET Core 3.1 WebAPI 中托管 Angular 应用程序? - How to host an Angular app inside .NET Core 3.1 WebAPI? WebApi .net core 3.1 - 如何自定义null发帖验证信息? - WebApi .net core 3.1 - How to customize validation message for null posting? 如何在Ubuntu上托管/发布我的.Net Core WebAPI? - How do I host/publish my .Net Core WebAPI on Ubuntu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM