1. Media Formatters
  2. Camel Case and Serializers
  3. Routes and Actions
  4. Attribute Routing
  5. Custom Content Negotiation

  

Media Formatters

  • Media type = MIME type
  • Examples: text/html, application/xml, application/json
  • Accept (what it wants), Content-Type (what it gets)
  • Built in formatters: XML, JSON, BSON, form-encoded
  • Create custom with MediaTypeFormatter, BufferedMediaTypeFormatter

Camel Case and Serializers

  • MediaFormatter performs serialization
  • Several SerializerSettings

Routes and Actions

Route template:

  • Api/{controller}/public/{resource}/{id}
    • controller = controller name
    • action = action (usually omitted)
  • Provide default values
    • defaults: new { resource = “widgets” }
  • Constrain
    • constraints: new { id = @”\d+” }
  • Optional
    • RouteParameter.Optional

Attribute Routing

  • Child resources
  • Add the attribute to the controller action
  • Must enable in configuration:
    • config.MapHttpAttributeRoutes();
  • Use RoutePrefix for a common prefix on the controller
    • tilde to override (i.e. “~/api/overrideprefix”)
    • May include parameters
    • Pass constraints (int, alpha, bool, etc.)

Custom Content Negotiation

  • MediaTypeMappings
    • QueryStringMapping
    • UriPathExtensionMapping
    • RequestHeaderMapping
  • Own Formatter
    • Derive from default, override MapRequestMediaType
    • Implement IContentNegotiator