Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Forked from darrelmiller/gist:3698177
Created September 11, 2012 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tugberkugurlu/3698733 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/3698733 to your computer and use it in GitHub Desktop.
A replacement for the old HttpResponseMessage<T>
public class HttpResponseMessage<T> : HttpResponseMessage
{
public HttpResponseMessage(HttpRequestMessage request, T value)
{
var config = request.GetConfiguration();
var contentNegotiator = config.Services.GetContentNegotiator();
var connegResult = contentNegotiator.Negotiate(
typeof(T), request, config.Formatters
);
request.CreateResponse(HttpStatusCode.Accepted, value);
Content = new ObjectContent<T>(value, connegResult.Formatter);
}
}
public class FooController : ApiController {
public HttpResponseMessage Get()
{
var value = new Foo()
{
A = "Hello",
B = "World"
};
return new HttpResponseMessage<Foo>(Request,value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment