I am creating a RESTful Web Service using ASP.NET MVC 4 Web API. For API access, I am returning JSON, though once I get everything working correctly, the content negotiation should work for XML and JSON by default.
Since I am working towards a truly RESTful resource-centric web service, my URI's will be pointing to actual resources. I would like to take advantage of that by returning an HTML representation of the resource if Accepts: text/html
comes in the request (like throwing the link in a browser).
I would like to be able to take advantage of MVC 4 Web API's content negotiation to insert a renderer for text/html that uses Razor templates. Are there any working examples of doing just this?
Yes, this is bridging "regular" MVC pages and Web API. Basically I'd like to create a renderer that uses a convention based approach to finding and rendering Razor views just like "regular" MVC. I can come up with the convention-based view lookup logic. I'm simply looking for a) globally inserting my text/html
renderer into the content negotation, and b) using the Razor engine manually to render my model into HTML.
You might take a look at WebApiContrib.Formatting.Razor. It's very similar to Kyle's answer, however it's a full-blown open source project with more features, unit tests, etc. You can get it on NuGet as well.
I will say it definitely needs more features, but they seem to have designed it well so it would be very easy to contribute to it.
Fredrik Normén has a blog post on this very topic:
http://weblogs.asp.net/fredriknormen/archive/2012/06/28/using-razor-together-with-asp-net-web-api.aspx
Basically, you need to create a
MediaTypeFormatter
and then register it in Global.asax:
the above code is copied from the blog post and is not my work