How about using a DelegatingHandler to override the acceptheader?
public class MediaTypeDelegatingHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var url = request.RequestUri.ToString();
//TODO: Maybe a more elegant check?
if (url.EndsWith(".json"))
{
// clear the accept and replace it to use JSON.
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}
else if (url.EndsWith(".xml"))
{
request.Headers.Accept.Clear();
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
}
return await base.SendAsync(request, cancellationToken);
}
}
And in your configuration:
GlobalConfiguration.Configuration.MessageHandlers.Add(new MediaTypeDelegatingHandler());
And your controller:
public class FooController : ApiController
{
public string Get()
{
return "test";
}
}
And if you go to http://yoursite.com/api/Foo/?.json
should return:
"test"
While http://yoursite.com/api/Foo/?.xml
should return
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">test</string>
Edit:
Note that you still need to handle the route parameter input, since the controller doesn't expect the .json-parameter. That's why the ?
may be necessary.