AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Clarification needed

Open mcurko opened this issue 6 years ago • 6 comments

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.

mcurko avatar Oct 27 '20 10:10 mcurko

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

Rick-Anderson avatar Oct 27 '20 21:10 Rick-Anderson

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!

mcurko avatar Oct 28 '20 08:10 mcurko

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.

serpent5 avatar Oct 28 '20 16:10 serpent5

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

mcurko avatar Oct 28 '20 17:10 mcurko

@serpent5 do you want this one?

Rick-Anderson avatar Oct 28 '20 18:10 Rick-Anderson

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

serpent5 avatar Oct 30 '20 22:10 serpent5