Skip to content
This repository has been archived by the owner on Nov 13, 2021. It is now read-only.

Provide guideline for content negotiation #135

Open
wants to merge 27 commits into
base: master
Choose a base branch
from

Conversation

FilipVanRaemdonck
Copy link

@FilipVanRaemdonck FilipVanRaemdonck commented Oct 9, 2019

Relates to #86, relates to #103

Copy link
Contributor

@tomkerkhove tomkerkhove left a 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

maturity-level-two/README.md Outdated Show resolved Hide resolved
maturity-level-two/README.md Outdated Show resolved Hide resolved
maturity-level-two/README.md Outdated Show resolved Hide resolved
maturity-level-two/README.md Outdated Show resolved Hide resolved
maturity-level-two/README.md Outdated Show resolved Hide resolved
maturity-level-two/README.md Outdated Show resolved Hide resolved
maturity-level-two/README.md Outdated Show resolved Hide resolved
@tomkerkhove tomkerkhove changed the title Add readme part for content negotiation + update the startup file Provide guideline for content negotiation Oct 9, 2019
maturity-level-two/README.md Outdated Show resolved Hide resolved
maturity-level-two/README.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@MassimoC MassimoC left a 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

maturity-level-two/README.md Outdated Show resolved Hide resolved
maturity-level-two/src/Startup.cs Outdated Show resolved Hide resolved
maturity-level-two/docs/content-negotiation.md Outdated Show resolved Hide resolved
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.
Copy link
Collaborator

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?

Copy link
Author

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..

Copy link
Author

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]

Copy link
Author

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

Copy link
Collaborator

@MassimoC MassimoC left a 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:
Copy link
Collaborator

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
Copy link
Collaborator

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

@MassimoC
Copy link
Collaborator

@FilipVanRaemdonck please also make sure the guideline cover the discussion #102

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants