-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
asp .net 5 can't return json with 404 status code #32046
Comments
Other http codes (4xx) return a valid json |
test project |
I did a little investigation and found that the problem occurs when we set the StatusCode = 404 in the response. In this case, the web server transmitting the response in "Transfer-Encoding: chunked" mode does not transmit a zero-length chunk at the end of the response body. Compare. As now:
As it should be:
@Tratcher, I'd like to take a deeper look at it. |
It's by design, you can disable this behavior with https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.exceptionhandleroptions.allowstatuscode404response?view=aspnetcore-5.0. |
But before it works on .net 5 :) Thanks for help |
@davidfowl It works with https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.exceptionhandleroptions.allowstatuscode404response?view=aspnetcore-5.0 |
Because we changed the behavior. https://docs.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/5.0/middleware-exception-handler-throws-original-exception We added this API later but never updated the original change it seems: |
@davidfowl It's okay that server return (failed)net::ERR_INCOMPLETE_CHUNKED_ENCODING ? |
That means the rethrown exception caused the connection to be closed and the response aborted. There's another issue where we disabled the 404 rethrow if there was a body to prevent this kind of abort. |
Correction, @JunTaoLuo and I talked about adding a HasStarted check here, but it doesn't look like it's happened yet. aspnetcore/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddleware.cs Line 135 in 9817a81
@JunTaoLuo do you remember if there's another issue for that, or did we change our minds? |
Thanks for contacting us. |
Triage: we think there is another issue for this. TLDR; we should add the HasStarted check on the body at the end of the error handler. |
Mark as up for grabs? |
Ah, the HasStarted check was proposed in #31024 (comment) but I neglected to do that since during triage we decided to add more useful exception message. I can add the check since it should be a one line change. |
I use .net 5.0.202 with system.text,json.
I have exception handler
With processing errors.
(failed)net::ERR_INCOMPLETE_CHUNKED_ENCODING
The text was updated successfully, but these errors were encountered: