如何在HttpGet中的模型Asp.net Core中传递数组属性?

时间:2017-10-17 10:52:19

标签: c# asp.net asp.net-core

我有Asp.Net WebApi 2 project, Now converted into Asp.Net Core 2.0

具有数组属性的HttpGet方法无法绑定模型。

该模型在Asp.net Core 2.0中为null (使用招摇测试)

是否需要进行任何更改?

public class Customer
{       
    public string name { get; set; }
    public new string[] systemId { get; set; }
}

CustomerController.cs

[HttpGet]
public IActionResult RetrieveData(Customer filters)
{
// code 

在Asp.net WebApi2它运行正常,但在Asp.net核心值无法绑定,值为空。

如果将其更改为[HttpPost],它将正常工作,但为什么不将[HttpGet]与ASP.Net WebApi2一样工作。

1 个答案:

答案 0 :(得分:1)

.net core将其默认绑定更改为FromBody。所以你的签名现在应该是:

[HttpGet]
public IActionResult RetrieveData([FromBody]Customer filters)
{
// code 

并将其作为正文发送。

您还可以使用数据属性:FromQuery

你可以阅读这篇文章:https://www.strathweb.com/2016/09/required-query-string-parameters-in-asp-net-core-mvc/

喝彩!