/// <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)); }
/// <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)); }
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)); }