首页 > 解决方案 > 如何为将字符串数组作为参数的 .Net Core 3.1 webapi 服务调用定义路由?

问题描述

我正在创建一个 .NetCore 3.1 Web API 服务。

我很难确定服务调用的正确路由定义。

考虑以下定义:

[HttpPut]
[Route("{paramA}/{paramB}")]
public void Sample(string paramA, string[] paramB)
{
}

传递字符串数组时定义路由参数的正确方法是什么?我应该改用 List 吗?

标签: c#.net-coreroutesasp.net-core-webapi

解决方案


我建议您可以通过查询字符串传递数组列表:

[HttpPut]   
[Route("{paramA}")]
public void Sample([FromRoute]string paramA, [FromQuery]string[] paramB)
{
}

发送请求,如:https://localhost:5001/xxxxxx/aaa?paramB=ss&paramB=sd

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


推荐阅读