| File: AcceptedOfT.cs | Web Access |
| Project: src\aspnetcore\src\Http\Http.Results\src\Microsoft.AspNetCore.Http.Results.csproj (Microsoft.AspNetCore.Http.Results) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Reflection; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http.Metadata; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Microsoft.AspNetCore.Http.HttpResults; /// <summary> /// An <see cref="IResult"/> that on execution will write an object to the response /// with status code Accepted (202) and Location header. /// Targets a registered route. /// </summary> public sealed class Accepted<TValue> : IResult, IEndpointMetadataProvider, IStatusCodeHttpResult, IValueHttpResult, IValueHttpResult<TValue> { /// <summary> /// Initializes a new instance of the <see cref="Accepted"/> class with the values /// provided. /// </summary> /// <param name="location">The location at which the status of requested content can be monitored.</param> /// <param name="value">The value to format in the entity body.</param> internal Accepted(string? location, TValue? value) { Value = value; Location = location; HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode); } /// <summary> /// Initializes a new instance of the <see cref="Accepted"/> class with the values /// provided. /// </summary> /// <param name="locationUri">The location at which the status of requested content can be monitored.</param> /// <param name="value">The value to format in the entity body.</param> internal Accepted(Uri locationUri, TValue? value) { Value = value; HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode); ArgumentNullException.ThrowIfNull(locationUri); if (locationUri.IsAbsoluteUri) { Location = locationUri.AbsoluteUri; } else { Location = locationUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped); } } /// <summary> /// Gets the object result. /// </summary> public TValue? Value { get; } object? IValueHttpResult.Value => Value; /// <summary> /// Gets the HTTP status code: <see cref="StatusCodes.Status202Accepted"/> /// </summary> public int StatusCode => StatusCodes.Status202Accepted; int? IStatusCodeHttpResult.StatusCode => StatusCode; /// <summary> /// Gets the location at which the status of the requested content can be monitored. /// </summary> public string? Location { get; } /// <inheritdoc/> public Task ExecuteAsync(HttpContext httpContext) { ArgumentNullException.ThrowIfNull(httpContext); if (!string.IsNullOrEmpty(Location)) { httpContext.Response.Headers.Location = Location; } // Creating the logger with a string to preserve the category after the refactoring. var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>(); var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.AcceptedResult"); HttpResultsHelper.Log.WritingResultAsStatusCode(logger, StatusCode); httpContext.Response.StatusCode = StatusCode; return HttpResultsHelper.WriteResultAsJsonAsync( httpContext, logger, Value); } /// <inheritdoc/> static void IEndpointMetadataProvider.PopulateMetadata(MethodInfo method, EndpointBuilder builder) { ArgumentNullException.ThrowIfNull(method); ArgumentNullException.ThrowIfNull(builder); builder.Metadata.Add(ProducesResponseTypeMetadata.CreateUnvalidated(typeof(TValue), StatusCodes.Status202Accepted, ContentTypeConstants.ApplicationJsonContentTypes)); builder.Metadata.Add(DisableCookieRedirectMetadata.Instance); } }