Aspnetcore.docs: Clarification needed

Created on 27 Oct 2020  Â·  6Comments  Â·  Source: dotnet/AspNetCore.Docs

_The preceding [Produces] filter:
Forces all actions within the controller to return JSON-formatted responses._

As per my tests, it doesn't. The "About" action as coded on this page will still return text/plain content.


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Source - Docs.ms doc-enhancement

Most helpful comment

The About action uses Content, which is described in this topic as specifying a text/plain response:

To return plain text formatted data, use ContentResult and the Content helper

Produces only really applies to ObjectResult. The scaffolded WeatherForecastController has a single Get action, which ends up using ObjectResult. If it were to use Content, or return a string, it would override the [Produces] attribute and return a text/plain response.

All 6 comments

That's because the About action is returning Razor, unlike a Web API.
Apply [Produces("text/plain")] to the weather controller, and it returns a 406

Perhaps if it's not able to convert to JSON, it returns the default.
cc @serpent5

Well if the filter under certain condition forces a result format while under other conditions it doesn't, and it's not clear what these conditions are, I wouldn't feel confident using it!

The About action uses Content, which is described in this topic as specifying a text/plain response:

To return plain text formatted data, use ContentResult and the Content helper

Produces only really applies to ObjectResult. The scaffolded WeatherForecastController has a single Get action, which ends up using ObjectResult. If it were to use Content, or return a string, it would override the [Produces] attribute and return a text/plain response.

OK, thanks, that's a good explanation, and what's missing in the doc!

@serpent5 do you want this one?

@Rick-Anderson Sure, but feel free to pick it up if you get time before I do.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AnthonyMastrean picture AnthonyMastrean  Â·  3Comments

wgutierrezr picture wgutierrezr  Â·  3Comments

danroth27 picture danroth27  Â·  3Comments

Raghumu picture Raghumu  Â·  3Comments

neman picture neman  Â·  3Comments