0

Let's say I have a custom extension .foo that implies the expected data type should be a "JSON-esque" document. For example, let's say I have 2 formatters that can support JSON, the standard JSON.Net and a custom JSON formatter that does kooky things to the JSON document. I only want to use the second JSON formatter for very specific api calls, and I can change the api endpoint in the client, so I want to do something like this:

[Route("~/api/Widgets/{id:int}.{format}")]
[ResponseType(typeof(Widget))]
public async Task<HttpResponseMessage> GetWidget(int id)
{
    //stuff goes here
}

as such, navigating to

https://myserver.com/api/Widgets/1234.foo

...should format the response with my "special" formatter for Widget 1234, but

https://myserver.com/api/Widgets/1234.json

...should return a standard JSON document.

How do I specify that my custom formatter only gets invoked when the format identifier is "foo"? What if I needed to support "foo" and "bar" formats with the same formatter?

I'd like to point out that I am well aware this seems like a silly thing to do, returning an essentially malformed JSON document. (trying to cut "alternative" or "best practice" answers off at the pass...)

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254

1 Answers1

0

I think this question covers exactly what you need:

Return either xml or json from MVC web api based on request

JSON/XML formatters are used there, however it works in the same way with custom formatters.

Community
  • 1
  • 1
Alexei Matrosov
  • 420
  • 4
  • 10