简体   繁体   English

C# / ASP.NET Core Web API如何传递参数?

[英]C# / ASP.NET Core Web API : how to pass a list parameter?

I'm trying to pass a list parameter to an ASP.NET Core 5 Web API.我正在尝试将列表参数传递给 ASP.NET Core 5 Web API。

Here is my endpoint:这是我的端点:

[ApiController]
[Route("[controller]")]
public class InvoiceController : ControllerBase
{
    private readonly IConfiguration _configuration;

    public InvoiceController(IConfiguration configuration)
    {
        _configuration = configuration;
    }

    [HttpPost]
    public List<CustomerInvoice> Post(string[] CustomerNumbers, DateTime OrderDateFrom, DateTime OrderDateTo)
    {
    }
}

Sending json post request fails with this error:发送 json 发布请求失败并出现以下错误:

The JSON value could not be converted to System.String[]. JSON 值无法转换为 System.String[]。 Path: $ |路径:$ | LineNumber: 0 |行号:0 | BytePositionInLine: 1.字节位置内线:1。

and here is the request:这是请求:

{
    "CustomerNumbers": ["test"],
    "OrderDateFrom": "2021-01-01",
    "OrderDateTo": "2021-11-02"
}

Instead of using a string array, I also tried List<string> .我也尝试了List<string> ,而不是使用字符串数组。 I got the same error.我得到了同样的错误。

Any idea why the framework does not understand how to receive a list?知道为什么框架不了解如何接收列表吗?

Because of the name of your parameter I suppose your array looks like this [2, 3, 5] but should look like this ["2", "3", "5"] .由于您的参数名称,我想您的数组看起来像这样[2, 3, 5]但应该看起来像这样["2", "3", "5"] It is expecting an array of strings and not an array of int.它需要一个字符串数组,而不是一个 int 数组。 Otherwise change the datatype to int[] CustomerNumbers否则将数据类型更改为int[] CustomerNumbers

You should create a class and receive json with this class.您应该创建一个 class 并使用此 class 接收 json。 This may solve your problem.这可能会解决您的问题。

    public class ClassExample
    {
        public List<string> CustomerNumbers { get; set; }
        public DateTime OrderDateFrom { get; set; }
        public DateTime OrderDateTo { get; set; }
    }



    [HttpPost]
    public List<CustomerInvoice> Post(ClassExample requestParameter)
    {
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 C# ASP.NET Core Web API 进行谷歌身份验证 - Google authentication with C# ASP.NET Core Web API C# ASP.NET Core Web API 包含与 where - C# ASP.NET Core Web API include with where 如何将表 OBJECT 输出参数从 C# ASP.NET 核心传递到 Z30162ED78B6C10F231411F2F4 - How to pass a TABLE OBJECT out parameter from C# ASP.NET Core to Oracle Stored Procedure 如何在ASP.NET Core Web API中传递int数组 - How to pass an array of int in ASP.NET Core Web API 如何使用 vba 将参数传递给 asp.net web api? - How to pass a parameter using vba to an asp.net web api? ASP.Net Core 6 HttpGet API ==&gt; 如何将 [FromQuery] 参数绑定到 C# object 数据类型 - ASP.Net Core 6 HttpGet API ==> How to bind [FromQuery] parameter to C# object data type 如何在 ASP.NET Core Web API 中发布对象列表 - How to post list of object in ASP.NET Core Web API ASP.NET Core Web API如何从客户端C#发送json对象 - asp.net core web api how to send json object from client side c# 如何使用 MongoDB 将动态 JSON 属性发布到 C# ASP.Net Core Web API? - How to post a dynamic JSON property to a C# ASP.Net Core Web API using MongoDB? C# ASP.NET Core Web API:如何在controller调用不同方法之间保持上下文 - C# ASP.NET Core Web API: How to maintain context in the controller between calling different methods
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM