I need to select OutputFormatter depending on the query parameter. How to do that?
I am moving from .NET Framework WebApi to .NET Core WebApi. The .NET Framework WebApi had DefaultContentNegotiator class do that:
public class CustomContentNegotiator : DefaultContentNegotiator
{
public override ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
//Read query from request object and add output formatters below
bindFormatters = new List<MediaTypeFormatter>
{
new ConvertResultRawFormatter(),
new JsonMediaTypeFormatter
{
SerializerSettings =
{
NullValueHandling = NullValueHandling.Ignore
}
}
};
}
return base.Negotiate(type, request, bindFormatters);
}
}
replace in configuration with new formatted negotiator
config.Services.Replace(typeof(IContentNegotiator), new CustomContentNegotiator());