I may be playing devils advocate here, but I can see the point why someone would want to do something like this.
Often it is nice to have an HTML representation of an API on the same URL. This approach allows users to click around and explore an API from within the browser.
I have gotten around this in the WebAPI by using a custom message handler which 302 redirects to an MVC route.
public class HtmlMessageHandler : DelegatingHandler
{
private List<string> contentTypes = new List<string> { "text/html", "application/html", "application/xhtml+xml" };
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request.Method == HttpMethod.Get && request.Headers.Accept.Any(h => contentTypes.Contains(h.ToString())))
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.Redirect);
var htmlUri = new Uri(String.Format("{0}/html",request.RequestUri.AbsoluteUri));
response.Headers.Location = htmlUri;
return Task.Factory.StartNew<HttpResponseMessage>(()=> response);
}
else
{
return base.SendAsync(request, cancellationToken);
}
}
}
Bit of a hack maybe but it does the job and I personally like it more than a custom HTML MediaTypeFormatter (which I also tried) ;)