Every formatter has a collection of MediaTypeMappings. These media type mappings give a way for you to indicate if you would want a particular formatter to take part in the process of content-negotation or not.
WebAPI ships with 4 mappings: QueryStringMapping, RequestHeaderMapping, UriPathExtensionMapping & MediaRangeMapping.
For your scenario, you could use the QueryStringMapping.
Example: you can add a media type mapping to the built-in XmlMediaTypeFormatter like this
config.Formatters.XmlFormatter.MediaTypeMapping.AddQueryStringMapping("frmt", "xml", new MediaTypeHeaderValue("application/xml"));
After this, you can make a request like this below and it will render the data in Xml format.
http://localhost/SampleApp/GetOrders?frmt=xml
Thanks,
Kiran Challa
Marked as answer by tugberk_ugurlu_ on Mar 02, 2012 06:32 PM
I assuming it is not that hard to create a custom mapping, right? What I really need is either quesrystring or RouteData but I favor RouteData. I'll try to create custom RouteDataMapping.
The returned double value is used by the conneg algorithm to find the appropriate formatter to write. Its similar to how you can set the quality value in Accept header.
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 05:54 PM|LINK
The current implementatio of MediaTypeFormatter works based on Accept header.
What is the suggested way of exposing data with the format based on querystring value or RouteData value?
tweets as @tourismgeek
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 06:04 PM|LINK
Every formatter has a collection of MediaTypeMappings. These media type mappings give a way for you to indicate if you would want a particular formatter to take part in the process of content-negotation or not.
WebAPI ships with 4 mappings: QueryStringMapping, RequestHeaderMapping, UriPathExtensionMapping & MediaRangeMapping.
For your scenario, you could use the QueryStringMapping.
Example: you can add a media type mapping to the built-in XmlMediaTypeFormatter like this
config.Formatters.XmlFormatter.MediaTypeMapping.AddQueryStringMapping("frmt", "xml", new MediaTypeHeaderValue("application/xml"));
After this, you can make a request like this below and it will render the data in Xml format.
http://localhost/SampleApp/GetOrders?frmt=xml
Kiran Challa
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 06:25 PM|LINK
This is great!
I assuming it is not that hard to create a custom mapping, right? What I really need is either quesrystring or RouteData but I favor RouteData. I'll try to create custom RouteDataMapping.
tweets as @tourismgeek
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 06:29 PM|LINK
yes...thats right...you can create a custom mapping...
Example : http://blogs.msdn.com/b/kiranchalla/archive/2012/02/27/content-negotiation-in-asp-net-mvc4-web-api-beta-part-2.aspx
Kiran Challa
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 06:32 PM|LINK
Nice! Perfect! Right on the spot! Thanks!
tweets as @tourismgeek
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 06:43 PM|LINK
I've a little problem.
I am unable to reach out to RouteData instance inside OnTryMatchMediaType.
tweets as @tourismgeek
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 06:47 PM|LINK
And what is the reason of returning double?
tweets as @tourismgeek
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 06:56 PM|LINK
I am not sure but can you check if the Request's Properties collection has this information?
Kiran Challa
Kiran Challa
Participant
1442 Points
281 Posts
Microsoft
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 06:58 PM|LINK
The returned double value is used by the conneg algorithm to find the appropriate formatter to write. Its similar to how you can set the quality value in Accept header.
Kiran Challa
tugberk_ugur...
Participant
1944 Points
1344 Posts
MVP
Re: MediaTypeFormatters based on QueryString or RouteData value instead of accept header
Mar 02, 2012 07:05 PM|LINK
I checked, couldn't find it. I will try to extract it from RouteCollection using RequestUri.
tweets as @tourismgeek