Requesting Other MIME Types

Excerpt by Phil Ledgerwood

MIME originally stood for Multipurpose Internet Mail Extensions to refer to the fact that emails would sometimes carry data that wasn't text, such as attached graphics or spreadsheets. MIME types were a way for email clients to recognize the different types of data coming across and handle them appropriately.

When dealing with web applications today, MIME types (also referred to as Content-Types) refer to all kinds of data that web clients access over the Internet. A common MIME type is text/html, but there's also text/css, application/xml, and even non-textual types such as application/pdf or image/gif.

JSON is the default MIME type that Web API services return, and it might fit a great many needs for web services, but what if your application needs to get XML from a service, or a comma-delimited file, or even a graphic or a spreadsheet?


XML is an extremely common data exchange format for web services and, as such, the Web API can support returning XML data with no code changes. All that needs to happen is that the HTTP request has to specify a Content-Type of application/xml.

If a Web API service gets an HTTP request with a Content-Type of application/xml, it will automatically serialize and return data as XML rather than JSON. Nothing special needs to be done to make this happen unless you want to provide a different XML serializer.

NOTE: At the time of this writing, the ASP.NET Web API uses the DataContractSerializer in the .NET Framework behind the scenes to do XML serialization. You can set a different serializer in the Web API's configuration.

Other MIME Types

JSON and XML are the only MIME types the Web API handles out of the box. However, the Web API can send back virtually any MIME type out there. If you want a Web API service to return something besides JSON or XML, you must first write your own "serializer" for the data you want to return. In Web API parlance, this is called a Media Formatter. This Media Formatter needs to be registered in the Web API configuration. Once this is done, any requests for that data that have a Content-Type of the MIME type of the appropriate Media Formatter will receive that type of data back from the service.

ldn-pledgerwoodThis post is an excerpt from the online courseware for our ASP.NET Web API Model Binding and Media Formats course written by expert Philip Ledgerwood.


Phil Ledgerwood

Philip Ledgerwood has been a software developer for fifteen years. He currently works primarily in .NET technologies producing custom software for organizations of all sizes. He has also done extensive training for those same organizations in both technical and business process topics. Philip is a strong advocate of Lean and agile software development and spends most of his time helping companies interested in the value those practices can bring to their development efforts. He does this through a combination of training and working "in the trenches" as a developer on these teams, keeping a hand in the academic side of emerging technology and practices while also directly applying it in real projects to bring real business value.

This course excerpt was originally posted March 15, 2013 from the online courseware ASP.NET Web API, Part 3 of 4: Model Binding and Media Formats by Phil Ledgerwood