public void Configuration(IAppBuilder app) { var httpConfiguration = new HttpConfiguration(); httpConfiguration.Formatters.Add(new BsonMediaTypeFormatter()); var defaultNegotiator = new DefaultContentNegotiator(excludeMatchOnTypeOnly: true); httpConfiguration.Services.Replace(typeof(IContentNegotiator), defaultNegotiator); httpConfiguration.MapHttpAttributeRoutes(); app.UseWebApi(httpConfiguration); }
public CustomContentNegotiator(Func<ContentNegotiatorContext, ContentNegotiationResult> action) { if (action == null) { throw new ArgumentNullException(nameof(action)); } _action = action; _defaultContentNegotiator = new DefaultContentNegotiator(); }
public void Configuration(IAppBuilder appBuilder) { var config = new HttpConfiguration(); var negotiator = new DefaultContentNegotiator(excludeMatchOnTypeOnly: true); config.Services.Replace(typeof(IContentNegotiator), negotiator); config.MapHttpAttributeRoutes(); appBuilder.UseWebApi(config); }
public static void WhenAcceptHeaderValuesHaveQualityThenNegotiatedMediaTypeMatchesHigherQuality(string mediaType1, double quality1, string mediaType2, double quality2, string expected) { var actual = new DefaultContentNegotiator().Negotiate( new[] { "text/xml", "text/json" }, new[] { new MediaTypeWithQualityHeaderValue(mediaType1, quality1), new MediaTypeWithQualityHeaderValue(mediaType2, quality2) }); Assert.AreEqual(expected, actual); }
public static void Register(HttpConfiguration config) { // responsible for returning the 406 when impossible to find a format for the Accept header DefaultContentNegotiator negotiator = new DefaultContentNegotiator(true); config.Services.Replace(typeof(IContentNegotiator), negotiator); //var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter; //xml.UseXmlSerializer = true; config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); config.Filters.Add((IFilter)DependencyResolver.Current.GetService(typeof(HttpWebApiResponseFilter))); // TODO: Fix dependency resolution //config.Filters.Add(new DataContextApiFilter()); config.Filters.Add((IFilter)DependencyResolver.Current.GetService(typeof(DataContextApiFilter))); }
public static void WhenAcceptHeaderStringHasQualityThenNegotiatedMediaTypeMatchesHigherQuality(string acceptHeader, string expected) { var actual = new DefaultContentNegotiator().Negotiate(new[] { "text/xml", "text/json" }, acceptHeader); Assert.AreEqual(expected, actual); }
public static void WhenAcceptHeaderIsMediaTypeThenNegotiatedMediaTypeMatches(string expected) { var actual = new DefaultContentNegotiator().Negotiate(new[] { "text/xml", "text/json" }, new[] { expected }); Assert.AreEqual(expected, actual); }