Unexpected invalid input when using ApiController attribute
I have a simple controller
[Route("[controller]")]
public class FooController : ControllerBase
{
private readonly IFooService service;
public FooController(IFooService service)
{
this.service = service;
}
[HttpGet]
[Route("{id:int}/{type}")]
public async Task<ActionResult<Foo>> Get(int id, string type, CancellationToken cancellationToken)
{
return await service.Get(id, type, Thread.CurrentPrincipal, cancellationToken);
}
}
which if invoked with http://localhost:5000/foo/1/bar it returns the appropriate Foo instance.
However, if I update this to use the new ApiController attribute in 2.1 i.e.
[Route("[controller]")]
[ApiController]
public class FooController : ControllerBase
{
...
}
I get a {"":["The input was not valid."]} response
From reading around this is most likely something to do with the JsonSerializer but I don't know how to work out what's going wrong - I've tried enabling logging but I don't get any output.
Might be the same issue as https://github.com/aspnet/Mvc/issues/7546. Did you set the compat version - see https://github.com/aspnet/Mvc/issues/7546#issuecomment-385037736?
I’ll have a go tomorrow but there’s no body just parameters that are part of the route
Sent from my iPhone
On 28 Jun 2018, at 17:24, Pranav K [email protected] wrote:
Might be the same issue as #7546. Did you set the compat version - see #7546 (comment)?
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
Right. The issue is that ApiController incorrectly classifies the CancellationToken as FromBody. Setting the compat version corrects the behavior.
I ran into this too, and it turns out I was missing SetCompatibilityVersion(CompatibilityVersion.Version_2_1) as well. Thanks @pranavkm, https://github.com/aspnet/Mvc/issues/7546 was helpful.
@phatcher feel free to reopen if you're still seeing this issue after setting the compat version.
@pranavkm Sorry, yes it worked this morning - I've raised a documentation issue to make it clearer
I added SetCompatibilityVersion(CompatibilityVersion.Version_2_1) in startup but still facing the same issue. the only diference here is i am using my own basecontroller that in turn inherits ControllerBase. Tried decorating the child alone and both base and child controllers with ApiController attribute but still the same error.
@ammar-haider Quick test would be to put a simple controller that directly inherits from ControllerBase to see if that works in your project
@phatcher Thanks for the reply. I did a quick test indeed and found the issue. In a controller without [ApiController] attribute, in a [HttpGet] operation if a complex object is passed i did not have to specify [FromQuery]. it automatically did so but with [ApiController] attribute i must explicitly specify [FromQuery] before the complex object parameter.
so this worked earlier:
public class TestController : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetValues( ComplexObject request)
{
return Ok(request);
}
}
but after applying [ApiController] it does not without [FromQuery].
Now it works like this:
[ApiController]
public class TestController : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetValues([FromQuery] ComplexObject request)
{
return Ok(request);
}
}
Hi, it looks like you are posting on a closed issue/PR/commit!
We're very likely to lose track of your bug/feedback/question unless you:
- Open a new issue
- Explain very clearly what you need help with
- If you think you have found a bug, include detailed repro steps so that we can investigate the problem
Thanks!