/// <summary> /// Executes the <see cref="ObjectResult"/>. /// </summary> /// <param name="context">The <see cref="ActionContext"/> for the current request.</param> /// <param name="result">The <see cref="ObjectResult"/>.</param> /// <returns> /// A <see cref="Task"/> which will complete once the <see cref="ObjectResult"/> is written to the response. /// </returns> public virtual Task ExecuteAsync(ActionContext context, ObjectResult result) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (result == null) { throw new ArgumentNullException(nameof(result)); } // If the user sets the content type both on the ObjectResult (example: by Produces) and Response object, // then the one set on ObjectResult takes precedence over the Response object if (result.ContentTypes == null || result.ContentTypes.Count == 0) { var responseContentType = context.HttpContext.Response.ContentType; if (!string.IsNullOrEmpty(responseContentType)) { if (result.ContentTypes == null) { result.ContentTypes = new MediaTypeCollection(); } result.ContentTypes.Add(responseContentType); } } var objectType = result.DeclaredType; if (objectType == null || objectType == typeof(object)) { objectType = result.Value?.GetType(); } var formatterContext = new OutputFormatterWriteContext( context.HttpContext, WriterFactory, objectType, result.Value); var selectedFormatter = FormatterSelector.SelectFormatter( formatterContext, (IList <IOutputFormatter>)result.Formatters ?? Array.Empty <IOutputFormatter>(), result.ContentTypes); if (selectedFormatter == null) { // No formatter supports this. Logger.NoFormatter(formatterContext); context.HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable; return(Task.CompletedTask); } Logger.ObjectResultExecuting(context); result.OnFormatting(context); return(selectedFormatter.WriteAsync(formatterContext)); }
private Task ExecuteAsyncCore(ActionContext context, ObjectResult result, Type objectType, object value) { var formatterContext = new OutputFormatterWriteContext( context.HttpContext, WriterFactory, objectType, value); var selectedFormatter = FormatterSelector.SelectFormatter( formatterContext, (IList <IOutputFormatter>)result.Formatters ?? Array.Empty <IOutputFormatter>(), result.ContentTypes); if (selectedFormatter == null) { // No formatter supports this. Logger.NoFormatter(formatterContext, result.ContentTypes); context.HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable; return(Task.CompletedTask); } Logger.ObjectResultExecuting(value); result.OnFormatting(context); return(selectedFormatter.WriteAsync(formatterContext)); }
public static void WhenAcceptHeaderValuesHaveQualityThenNegotiatedMediaTypeMatchesHigherQuality(string mediaType1, double quality1, string mediaType2, double quality2, string expected) { var actual = new FormatterSelector().Negotiate( new[] { "text/xml", "text/json" }, new[] { new MediaTypeWithQualityHeaderValue(mediaType1, quality1), new MediaTypeWithQualityHeaderValue(mediaType2, quality2) }); Assert.AreEqual(expected, actual); }
/// <summary> /// Executes the <see cref="ObjectResult"/>. /// </summary> /// <param name="context">The <see cref="ActionContext"/> for the current request.</param> /// <param name="result">The <see cref="ObjectResult"/>.</param> /// <returns> /// A <see cref="Task"/> which will complete once the <see cref="ObjectResult"/> is written to the response. /// </returns> public virtual Task ExecuteAsync(ActionContext context, ObjectResult result) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (result == null) { throw new ArgumentNullException(nameof(result)); } InferContentTypes(context, result); var objectType = result.DeclaredType; if (objectType == null || objectType == typeof(object)) { objectType = result.Value?.GetType(); } var formatterContext = new OutputFormatterWriteContext( context.HttpContext, WriterFactory, objectType, result.Value); var selectedFormatter = FormatterSelector.SelectFormatter( formatterContext, (IList <IOutputFormatter>)result.Formatters ?? Array.Empty <IOutputFormatter>(), result.ContentTypes); if (selectedFormatter == null) { // No formatter supports this. Logger.NoFormatter(formatterContext); context.HttpContext.Response.StatusCode = StatusCodes.Status406NotAcceptable; return(Task.CompletedTask); } Logger.ObjectResultExecuting(result.Value); result.OnFormatting(context); return(selectedFormatter.WriteAsync(formatterContext)); }
public static Tuple <MediaTypeFormatter, MediaTypeHeaderValue> Negotiate <T>( this MediaTypeFormatterCollection formatters, HttpRequestMessage requestMessage) { var formatSelector = new FormatterSelector(); MediaTypeHeaderValue mediaType = null; var response = new HttpResponseMessage() { RequestMessage = requestMessage }; var formatter = formatSelector.SelectWriteFormatter( typeof(T), new FormatterContext(response, false), formatters, out mediaType); return (new Tuple <MediaTypeFormatter, MediaTypeHeaderValue>( formatter, mediaType )); /* * Usage: * public HttpResponseMessage Get() { * * var response = new HttpResponseMessage(); * * var contact = new Contact() {FirstName = "Joe", LastName = "Brown"}; * * var mediaInfo = Configuration.Formatters.Negotiate<Contact>(Request); * * response.Content = new SimpleObjectContent<Contact>(contact, mediaInfo.Item1); * response.Content.Headers.ContentType = mediaInfo.Item2; * * return response; * } */ }
private Task ExecuteAsyncCore(ActionContext context, ObjectResult result, Type?objectType, object?value) { var formatterContext = new OutputFormatterWriteContext( context.HttpContext, WriterFactory, objectType, value); var selectedFormatter = FormatterSelector.SelectFormatter( formatterContext, (IList <IOutputFormatter>)result.Formatters ?? Array.Empty <IOutputFormatter>(), result.ContentTypes); if (selectedFormatter == null) { // No formatter supports this. Log.NoFormatter(Logger, formatterContext, result.ContentTypes); const int statusCode = StatusCodes.Status406NotAcceptable; context.HttpContext.Response.StatusCode = statusCode; if (context.HttpContext.RequestServices.GetService <IProblemDetailsService>() is { } problemDetailsService) { return(problemDetailsService.WriteAsync(new() { HttpContext = context.HttpContext, ProblemDetails = { Status = statusCode } }).AsTask()); } return(Task.CompletedTask); } Log.ObjectResultExecuting(Logger, result, value); result.OnFormatting(context); return(selectedFormatter.WriteAsync(formatterContext)); }
public static void WhenAcceptHeaderStringsHaveQualityThenNegotiatedMediaTypeMatchesHigherQuality(string mediaType1, string mediaType2, string expected) { var actual = new FormatterSelector().Negotiate(new[] { "text/xml", "text/json" }, new[] { mediaType1, mediaType2 }); Assert.AreEqual(expected, actual); }
public static void WhenAcceptHeaderIsMediaTypeThenNegotiatedMediaTypeMatches(string expected) { var actual = new FormatterSelector().Negotiate(new[] { "text/xml", "text/json" }, new[] { expected }); Assert.AreEqual(expected, actual); }