Skip to content

Instantly share code, notes, and snippets.

@tugberkugurlu
Created June 21, 2012 12:26
Show Gist options
  • Save tugberkugurlu/2965468 to your computer and use it in GitHub Desktop.
Save tugberkugurlu/2965468 to your computer and use it in GitHub Desktop.
Running conneg manually inside a controller - ASP.NET Web API
public class CarsController : ApiController {
public string[] Get() {
var cars = new string[] {
"Car 1",
"Car 2",
"Car 3"
};
var contentNegotiator = Configuration.Services.GetContentNegotiator();
var connegResult = contentNegotiator.Negotiate(typeof(string[]), Request, Configuration.Formatters);
//you have the proper formatter for your request in your hand
var properFormatter = connegResult.Formatter;
//you have the proper MediaType for your request in your hand
var properMediaType = connegResult.MediaType;
return cars;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment