3

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?

1
  • +1, interesting question. I would also like to know if this is possible without using the api prefix in the routes. But I suspect that this won't be an easy thing to implement. Commented Sep 21, 2012 at 16:46

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

I built this site hypermediaapi.com as proof of concept that you can build web sites using just web api. It uses Razor templates and twitter bootstrap. If you hit hypermediaapi.com/learning with text/plain in your accept header it will conneg.
Thanks Chris and @Darrel. It does seem that this is the way to go. There is an additional schema complication I need to add on to the request (so is requesting as per expected schema version), but if it works ... Such a missed opportunity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.