Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting Response Data #122

Closed
danroth27 opened this issue Apr 21, 2015 · 3 comments
Closed

Formatting Response Data #122

danroth27 opened this issue Apr 21, 2015 · 3 comments
Assignees
Labels
Pri0 Urgent priority
Milestone

Comments

@danroth27
Copy link
Member

danroth27 commented Apr 21, 2015

ASP.NET MVC -> Models -> Formatting Response Data

  • Format specific action results
    • JsonResult
    • ContentResult
      • string (will just return text)
    • Note: For non-trivial actions with multiple return types, prefer IActionResult
  • Content negotiation
    • Define content negotiation - only happens if client specifies Accept header
    • Default format is JSON
    • IActionResult and Ok, BadRequest, Created, etc.
    • Return a POCO/Model Type (e.g. Foo, IEnumerable) (framework creates an ObjectResult for you - note null behavior returns 204 NoContent)
    • [Produces("application/json")] filter/attribute to restrict a controller or action (or global) to a particular content type
    • Note how browser accept headers are handled by default
  • Configuring formatters
    • Xml Formatters package
    • config.InputFormatters, config.OutputFormatters in AddMvc (only outputformatters matter for conneg; input formatters would affect model binding)
    • builder.AddXmlSerializerFormatters (in formatters.xml package)
    • Use config.RespectBrowserAcceptHeader to support browser/wildcard content negotiation (if there is a wildcard, we will assume it's a browser)
    • Add camel casing to Json formatting using AddJsonOptions and SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    • Special case for return string (will be text/plain unless TextPlainFormatter removed)
    • Special case for return null (as a POCO, not an IActionResult) (will be 204 NoContent unless HttpNoContentOutputFormatter is removed)
    • If you give us a header, we go through list of formatters, find one that can serialize the type to that format.
    • If no formatter can support the type being returned, then we will send a 406
      • If you ask for XML, but we don't have it, we will return JSON
    • If no accept header is given, we will go through the formatters in order and return the first one that can handle the object to be serialized
  • Response format URL mappings
  • Recommendations
    • Prefer IActionResult as your return type for data since it allows you to handle error states and different HTTP status codes

Note: Chrome's Accept header includes application/xml by default, so if you enable Xml and you find you're getting it when you hit your endpoint, that's probably why. If accept header has a wildcard (*), which browsers tend to do, then we will assume it's a browser and will send JSON. If you are not respecting browser headers, and there is a wildcard, we will just send JSON (we will act as if there was no Accept header).

Does ASP.NET Core respect q factors? http://stackoverflow.com/questions/8552927/what-is-q-0-5-in-accept-http-headers
YES. Link to RFC and just say we support the spec.
"Content negotiation allows clients to use the accept header to tell the server what formats they accept" (link accept header to RFC)

Reference: https://blogs.msdn.microsoft.com/webdev/2014/11/24/content-negotiation-in-mvc-6-or-how-can-i-just-write-json/

@danroth27 danroth27 added this to the 1.0.0-rc2 milestone Dec 15, 2015
@danroth27 danroth27 added mvc and removed 1 - Ready labels Dec 15, 2015
@ardalis ardalis self-assigned this Jan 16, 2016
@danroth27 danroth27 added the Pri0 Urgent priority label Feb 16, 2016
@ardalis
Copy link
Contributor

ardalis commented Apr 12, 2016

@danroth27 I fleshed this out a bit. I'm not sure what "Response format URL mappings" means, though. Can you clarify?

@danroth27
Copy link
Member Author

Response format mappings allow you to specify some part of the URI, like a format query string parameter, or a format specific file extension (.xml, .json) as a way to indicate that you would like a specific response format.

@danroth27 danroth27 added the TOC label May 19, 2016
@danroth27 danroth27 modified the milestones: 1.0.0-rc2, 1.0.0 May 22, 2016
trobinson41 pushed a commit to trobinson41/Docs that referenced this issue May 26, 2021
Jan 15 - media, initial transform, rate limits, db increment, nav, perf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pri0 Urgent priority
Projects
None yet
Development

No branches or pull requests

2 participants