1

I have Asp.Net WebApi 2 project, Now converted into Asp.Net Core 2.0

HttpGet method with array property not able to bind model.

the model comes null in Asp.net Core 2.0 (using swagger to test)

Any changes required?

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

CustomerController.cs

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

In Asp.net WebApi2 It was working properly, but in Asp.net Core values are not able to bind, values are null.

If change it to [HttpPost] it will work fine but why not with [HttpGet] as working like ASP.Net WebApi2.

3
  • 1
    provide a minimal reproducible example that can be used to reproduce the problem. include example request. Commented Oct 17, 2017 at 10:55
  • 1
    @Neo, how do you test the request? I think you should do: GET YourApi/RetrieveData?name=SomeName&system_id=1&system_id=2 Commented Mar 26, 2018 at 9:49
  • yes i will try that way thanks. Commented Mar 26, 2018 at 10:54

1 Answer 1

1

.net core changed it default binding to FromBody. So your signature should now be:

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

And send it as body.

You could also use the data-attribute: FromQuery.

you can read this article: https://www.strathweb.com/2016/09/required-query-string-parameters-in-asp-net-core-mvc/

cheers!

Sign up to request clarification or add additional context in comments.

9 Comments

a GET request has no body
ahh, correct, misread it! but the FromQuery would still apply
You are close to the answer. review this learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding and reformulate your answer.
both properties are empty? how does your url look like?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.