I have the following issue. Whenever I send something to my Api endpoint, ASP.NET Core 3.1 is not able to process the request. However, when I add the ApiController
attribute it works perfectly fine.
My code is correct but only works when I add this attribute. How is that so?
For reference, here's my code
API
[ApiController] //Remove this and the code breaks
[Route(api/SomeApi)]
public class ApiController : Controller {
private readonly IService service;
public ApiController(IService service)
{
this.service = service;
}
[HttpPost]
[Route(Add)]
public SomeClass Add(SomeClass foo)
{
var userId = service.GetCurrentUserId(User);
foo.Id = Guid.NewGuid();
foo.UserId = userId;
service.Add(foo);
return foo;
}
}
JS
axios.post('/api/SomeApi/Add', {
foo: this.foo,
}).then(function (response: any) {
this.Id = response.Id;
});
FYI, I have other methods on my ApiController using GET/POST. The GET ones work perfectly fine but the POST methods only work when I use query parameters. In this case, I didn't use query parameters because I have more data to send to my Api than actually given in the example.
I've already tried to get my response using [FromBody]
. It did not work. I instead got null. foo
was not even instantiated.