Skip to content

Make IResults types public #40704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 30 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0ab4de9
StatuCodeResult as base class
brunolins16 Mar 9, 2022
813786e
StatuCodeResult as base class
brunolins16 Mar 9, 2022
811c604
Making types public and renaming to HTTPResult
brunolins16 Mar 10, 2022
5d66c24
Update comments
brunolins16 Mar 11, 2022
acf0a3b
Make setters internal
brunolins16 Mar 11, 2022
9ddf586
Updating public API file
brunolins16 Mar 11, 2022
9a79b02
Removing base classes
brunolins16 Mar 14, 2022
a1fef75
Changing method name to WriteResultAsStatusCode
brunolins16 Mar 14, 2022
1ee3138
Updating API
brunolins16 Mar 14, 2022
2c029a8
code cleanup
brunolins16 Mar 14, 2022
4103a0f
fixing public api
brunolins16 Mar 15, 2022
5fdebc4
Simplyfing interfaces
brunolins16 Mar 15, 2022
6146e4a
Removing interfaces + Adding ctors
brunolins16 Mar 18, 2022
7d89ce3
Fixing xml docs
brunolins16 Mar 18, 2022
6171f15
Making EmptyResult public
brunolins16 Mar 18, 2022
6a807cf
Updating JsonHttpResult
brunolins16 Mar 18, 2022
5fa233d
Adding missing ctor
brunolins16 Mar 18, 2022
dcf745b
Adding missing ctor
brunolins16 Mar 18, 2022
3d01bb6
Adding missing ctor
brunolins16 Mar 18, 2022
e05b04f
Adding misssed xml comment
brunolins16 Mar 18, 2022
9c628aa
Removing configureResponseHeader
brunolins16 Mar 18, 2022
eada9b4
PR Feedback
brunolins16 Mar 18, 2022
ff3cb76
Moving ctors to internal
brunolins16 Mar 18, 2022
00877b5
Create logger using string category
brunolins16 Mar 18, 2022
825f4c0
Adding more unittests
brunolins16 Mar 18, 2022
73800b9
Update src/Http/Http.Results/src/EmptyHttpResult.cs
brunolins16 Mar 18, 2022
8fb15dd
Update ContentHttpResult.cs
brunolins16 Mar 18, 2022
dac8711
avoid closure allocation
brunolins16 Mar 18, 2022
4a50104
Merge branch 'brunolins16/iresult-apisuggestion' of https://github.co…
brunolins16 Mar 18, 2022
1d76b6e
unit loggerfactory
brunolins16 Mar 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Http;

using System.Threading.Tasks;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Microsoft.AspNetCore.Http.Result;

internal sealed class AcceptedAtRouteResult : ObjectResult
/// <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 AcceptedAtRouteHttpResult : IResult
{
/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtRouteResult"/> class with the values
/// Initializes a new instance of the <see cref="AcceptedAtRouteHttpResult"/> class with the values
/// provided.
/// </summary>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
public AcceptedAtRouteResult(object? routeValues, object? value)
internal AcceptedAtRouteHttpResult(object? routeValues, object? value)
: this(routeName: null, routeValues: routeValues, value: value)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedAtRouteResult"/> class with the values
/// Initializes a new instance of the <see cref="AcceptedAtRouteHttpResult"/> class with the values
/// provided.
/// </summary>
/// <param name="routeName">The name of the route to use for generating the URL.</param>
/// <param name="routeValues">The route data to use for generating the URL.</param>
/// <param name="value">The value to format in the entity body.</param>
public AcceptedAtRouteResult(
internal AcceptedAtRouteHttpResult(
string? routeName,
object? routeValues,
object? value)
: base(value, StatusCodes.Status202Accepted)
{
Value = value;
RouteName = routeName;
RouteValues = new RouteValueDictionary(routeValues);
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
}

/// <summary>
/// Gets the object result.
/// </summary>
public object? Value { get; }

/// <summary>
/// Gets the name of the route to use for generating the URL.
/// </summary>
Expand All @@ -46,12 +59,17 @@ public AcceptedAtRouteResult(
/// </summary>
public RouteValueDictionary RouteValues { get; }

/// <inheritdoc />
protected override void ConfigureResponseHeaders(HttpContext context)
/// <summary>
/// Gets the HTTP status code.
/// </summary>
public int StatusCode => StatusCodes.Status202Accepted;

/// <inheritdoc/>
public Task ExecuteAsync(HttpContext httpContext)
{
var linkGenerator = context.RequestServices.GetRequiredService<LinkGenerator>();
var linkGenerator = httpContext.RequestServices.GetRequiredService<LinkGenerator>();
var url = linkGenerator.GetUriByAddress(
context,
httpContext,
RouteName,
RouteValues,
fragment: FragmentString.Empty);
Expand All @@ -61,6 +79,11 @@ protected override void ConfigureResponseHeaders(HttpContext context)
throw new InvalidOperationException("No route matches the supplied values.");
}

context.Response.Headers.Location = url;
// 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.AcceptedAtRouteResult");

httpContext.Response.Headers.Location = url;
return HttpResultsHelper.WriteResultAsJsonAsync(httpContext, logger, Value, StatusCode);
}
}
88 changes: 88 additions & 0 deletions src/Http/Http.Results/src/AcceptedHttpResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Http;

using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

/// <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 AcceptedHttpResult : IResult
{
/// <summary>
/// Initializes a new instance of the <see cref="AcceptedHttpResult"/> 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 AcceptedHttpResult(string? location, object? value)
{
Value = value;
Location = location;
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
}

/// <summary>
/// Initializes a new instance of the <see cref="AcceptedHttpResult"/> 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 AcceptedHttpResult(Uri locationUri, object? value)
{
Value = value;
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);

if (locationUri == null)
{
throw new ArgumentNullException(nameof(locationUri));
}

if (locationUri.IsAbsoluteUri)
{
Location = locationUri.AbsoluteUri;
}
else
{
Location = locationUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);
}
}

/// <summary>
/// Gets the object result.
/// </summary>
public object? Value { get; }

/// <summary>
/// Gets the HTTP status code.
/// </summary>
public int StatusCode => StatusCodes.Status202Accepted;

/// <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)
{
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");
return HttpResultsHelper.WriteResultAsJsonAsync(
httpContext,
logger,
Value,
StatusCode);
}
}
66 changes: 0 additions & 66 deletions src/Http/Http.Results/src/AcceptedResult.cs

This file was deleted.

49 changes: 49 additions & 0 deletions src/Http/Http.Results/src/BadRequestObjectHttpResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.AspNetCore.Http;

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

/// <summary>
/// An <see cref="IResult"/> that on execution will write an object to the response
/// with Bad Request (400) status code.
/// </summary>
public sealed class BadRequestObjectHttpResult : IResult
{
/// <summary>
/// Initializes a new instance of the <see cref="BadRequestObjectHttpResult"/> class with the values
/// provided.
/// </summary>
/// <param name="error">The error content to format in the entity body.</param>
internal BadRequestObjectHttpResult(object? error)
{
Value = error;
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
}

/// <summary>
/// Gets the object result.
/// </summary>
public object? Value { get; internal init; }

/// <summary>
/// Gets the HTTP status code.
/// </summary>
public int StatusCode => StatusCodes.Status400BadRequest;

/// <inheritdoc/>
public Task ExecuteAsync(HttpContext httpContext)
{
// 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.BadRequestObjectResult");

return HttpResultsHelper.WriteResultAsJsonAsync(
httpContext,
logger: logger,
Value,
StatusCode);
}
}
12 changes: 0 additions & 12 deletions src/Http/Http.Results/src/BadRequestObjectResult.cs

This file was deleted.

Loading