-
Notifications
You must be signed in to change notification settings - Fork 5
Provide guideline for content negotiation #135
base: master
Are you sure you want to change the base?
Provide guideline for content negotiation #135
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added some comments to streamline it, the content is a bit too much documentation oriented rather than guideline and straight to the point
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some comments added. Make sure your long explanation is in sync with the short bullet list
options.OutputFormatters.Add(jsonOutputFormatter); | ||
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2); | ||
``` | ||
In here we have to add the 'SetCompatibilityVersion'as well to make sure the supported formats are documented correctly in e.g. the swagger. In case you'd like to add other formatting possibilities, it is possible to add these formatters to your api formatters. In case of xml you can use the XmlSerializerInputFormatter and the XmlSerializerOutputFormatter or add the xml formatters using the Mvc. With this approach the default format is still JSON. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One question : just imagine that i want to return XML for only a single operation. What should i do?
A better scenario is : I have an API with multiple operations, one of those is a GET that returns a list of items.
I want to be able to get the list of items in "text/csv" what should be the approach?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can uses the [Produces("text/csv") but this will not allow the "text/csv" as an acceptable "accept' header..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry that was a mistake, you just shouldn't return an ok(...) and then you can work with the [Produces]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I now added some information about it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some minor changes have been discussed this morning. Waiting for the next update. Thx
public async Task<IActionResult> CreateCar([FromBody] NewCarRequest newCarRequest) | ||
``` | ||
|
||
In case you have an action which returns media type(s) only this action will return, you can use the [Produces] and [Consumes] keywords too. But be aware that in this case your api might not know how it should serialize the response, so you might have to take care of this yourself. In order to do so you can return a ContentResult (e.g. FileContentResult or ContentResult in the Microsoft.AspNetCore.Mvc namespace). An example is given below: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In case you have an action which returns media type(s) only this action will return, you can use the [Produces] and [Consumes] keywords too.
Not clear
} | ||
``` | ||
|
||
## Error response codes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why "codes"? It's more error response format
@FilipVanRaemdonck please also make sure the guideline cover the discussion #102 |
Relates to #86, relates to #103