Closed maurei closed 4 years ago
@maurei thanks for contacting us.
@pranavkm Can you tell what's going on here/what's missing?
Do you have an [ApiController]
annotation in the first case? If not, that might explain the difference - controller instances annotated with ApiController
attribute transform status code results in to ObjectResults
with ProblemDetails
- https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.0#problem-details-for-error-status-codes. In the absence of the ApiController
annotation, all you have is a regular status code result which does not need to be formatted.
We're using conventional routing because we want to programmatically configure routing for better extensibility and at the same time we want to use a custom formatter to add additional error details when an entity is not found.
Why not add a result filter that transforms NotFoundResult
\ NotFoundObjectResult
to return a custom result type?
Thanks for your time guys, greatly appreciated
Yes the lack of [ApiController]
is one case is the difference, see the repro repository.
all you have is a regular status code result which does not need to be formatted.
I.e. the formatters not being executed is by design? That troubles me a bit from an intuitive point of view.
return NotFound()
versus return NotFound(content)
in the presence or absence of [ApiController]
. (Is there documentation about this?) I feel like switching from attribute routing to conventional routing shouldn't affect the middleware any other than what is necessary for routingWhy not add a result filter that transforms NotFoundResult \ NotFoundObjectResult to return a custom result type?
I guess I could. But is this the intended way to deal with this? My concerns with this are:
[ApiController]
Thanks again for your time 👍
Any update on this?
.e. the formatters not being executed is by design?
Yes, formatters are only invoked when there are objects to serialize. There are plenty of action result types, including StatusCodeResult
, FileStreamResult
, ViewResult
, ChallengeResult
etc, that do not incur a formatter being invoked.
The result filters aren't executed in the case of exceptions or short-circuit because of authorization
You should use an IAlwaysRunResultFilter
.
Thank you for contacting us. Due to no activity on this issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.
Description
I'm using conventional routing: I'm programmatically assigning templates to controllers, ie not using
[ApiController][Route(" .. ")
but anIApplicationModelConvention
implementation. Also, I have a customIOutputFormatter
. The bug is that my custom formatter is not always executed: it depends on how my routing is configured. Below is my controller in which I use conventional routingWhen triggering the first and second actions my formatter is executed. For the last action my custom formatter is entirely skipped.
This behaviour is different when using attribute based routing. In this case, for all three actions below my custom outputter is executed:
This behaviour being different is unexpected and I believe it is a bug.
To Reproduce
Clone this repro repository. Run it and check out and hit the endpoints
All of them should return the same output but this is not the case.
Use-case
In case the reader is wondering why this is relevant: I'm working on a framework that implements json:api. We're using conventional routing because we want to programmatically configure routing for better extensibility and at the same time we want to use a custom formatter to add additional error details when an entity is not found.
The work-around for now is to return
NotFound(null)
instead ofNotFound()
.Further technical details