There are a few ways.
A) You can just make the client send the correct accept header so that Web API negotiates the correct formatters.
b) You can customize the configuration on a per controller basis. This would allow you to define the HTML formatter only for the controllers where you want to return HTML. Here is a link on how to do per controller config: http://blogs.msdn.com/b/jmstall/archive/2012/05/11/per-controller-configuration-in-webapi.aspx
c) Don't use the return pipeline conneg for the HTML controllers. Return a HttpResposeMessage and set the Content property explicitly.
public class HomeController : ApiController {
//http://localhost:8080/api/home
public HttpResponseMessage GetValues() {
var value = Value() { Numbers = new int[] { 1, 2, 3 } };
return new HttpResponseMessage() {
Content = new ObjectContent<Value>(value, new HtmlMediaTypeViewFormatter())
};
}
//want to return just XML and not go via the view+razor+cshtml
//Works ok for normal self-hosted webapi, but cant get to work for views+razor
//http:localhost:8080/api/home/1
public String Get2(int id) {
return "Get2() returns a string";
}
}