0

This is my controller's action for POST api/foo?bar=true requests:

[HttpPost]
[Route("api/foo")]
public async Task<HttpResponseMessage> Update(bool bar)
{}

It works fine, but when I try to do the same with a query parameter with no value like POST api/foo?bar, the action above doesn't work and this message is returned:

The parameters dictionary contains a null entry for parameter 'bar' of non-nullable type 'System.Boolean' for method 'System.Threading.Tasks.Task`[System.Net.Http.HttpResponseMessage] Update(Boolean)' in 'Foo.Controllers.FooController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.

How can I make requests like POST api/foo?bar get into the Update() action? Optional parameter/Other type for the parameter?

Any help is welcome! Thanks.

1 Answer 1

0

Solved!

Made the parameter nullable leaving the action like this:

[HttpPost]
[Route("api/foo")]
public async Task<HttpResponseMessage> Update(bool? bar)
{}
Sign up to request clarification or add additional context in comments.

Comments

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.