I have been working on a Web API which I am hoping to be RESTful. To give an example:
http://product.domain.local/person
would give a a web page with a nice UI served by ASP.NET MVC (for humans).
However, if I ask for Accept json/xml in my HTTP header, I’d like to redirect the request to my Web API controller. ie. as well as MapHttpRoute, I want to map based on contentType requested.
Therefore, when my API returns an ID:
http://product.domain.local/person/1
If a human uses it, they get a web page, if a client application uses it, they get JSON/XML (because they’d modify the Accept header), which allows me to use HTTP redirection, etc. to work towards REST.
But I can’t find a way to do this. Is it even possible?
I’ve found this to be quite difficult. Webapi is really good at routing based on the accept type header, but isn’t good at dealing with views. MVC is good at views but not as good at handling various responses.
If anything, I think it is easiest to make MVC is easiest. Depending on the flexibility you need for the accept type handling, you could probably just implement a common function (that returns an
ActionResult) and in it just return the proper type based on your head (or anything else you would like).