I'm not quite sure how much your two JSONs differ and what you do with them, but if you ask me, I will do this in the format:
public class MyJsonMediaTypeFormatter : JsonMediaTypeFormatter { private IHttpRouteData _route; public override MediaTypeFormatter GetPerRequestFormatterInstance(Type type, HttpRequestMessage request, System.Net.Http.Headers.MediaTypeHeaderValue mediaType) { _route = request.GetRouteData(); return base.GetPerRequestFormatterInstance(type, request, mediaType); } public override System.Threading.Tasks.Task WriteToStreamAsync(Type type, object value, System.IO.Stream writeStream, HttpContent content, TransportContext transportContext) { if (_route.Route.RouteTemplate.Contains("legacy")) {
Then you replace the default JsonMEdiaTypeFormatter with this.
config.Formatters.RemoveAt(0); config.Formatters.Insert(0, new MyJsonMediaTypeFormatter());
In the web API, you can have a DelegatingHandler that only works on a specific route, but that doesn't make sense, since the Formatters collection is global, so it makes no sense to change this at runtime even from a route, area handler.
source share