Clarification needed
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.
- ID: 6ebcd752-af71-c483-eebb-0745bd04fb30
- Version Independent ID: 1f678d5d-230b-0ad0-c925-15b13081f553
- Content: Format response data in ASP.NET Core Web API
- Content Source: aspnetcore/web-api/advanced/formatting.md
- Product: aspnet-core
- Technology: aspnetcore-webapi
- GitHub Login: @ardalis
- Microsoft Alias: riande
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
ContentResultand theContenthelper
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.