Skip to content

Commit db7d513

Browse files
Make IResults types public (#40704)
* StatuCodeResult as base class * StatuCodeResult as base class * Making types public and renaming to HTTPResult * Update comments * Make setters internal * Updating public API file * Removing base classes * Changing method name to WriteResultAsStatusCode * Updating API * code cleanup * fixing public api * Simplyfing interfaces * Removing interfaces + Adding ctors * Fixing xml docs * Making EmptyResult public * Updating JsonHttpResult * Adding missing ctor * Adding missing ctor * Adding missing ctor * Adding misssed xml comment * Removing configureResponseHeader * PR Feedback * Moving ctors to internal * Create logger using string category * Adding more unittests * Update src/Http/Http.Results/src/EmptyHttpResult.cs Co-authored-by: Brennan <[email protected]> * Update ContentHttpResult.cs * avoid closure allocation Co-authored-by: Brennan <[email protected]>
1 parent 313ee06 commit db7d513

File tree

81 files changed

+2926
-1519
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2926
-1519
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,54 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
namespace Microsoft.AspNetCore.Http;
5+
6+
using System.Threading.Tasks;
47
using Microsoft.AspNetCore.Routing;
58
using Microsoft.Extensions.DependencyInjection;
9+
using Microsoft.Extensions.Logging;
610

7-
namespace Microsoft.AspNetCore.Http.Result;
8-
9-
internal sealed class AcceptedAtRouteResult : ObjectResult
11+
/// <summary>
12+
/// An <see cref="IResult"/> that on execution will write an object to the response
13+
/// with status code Accepted (202) and Location header.
14+
/// Targets a registered route.
15+
/// </summary>
16+
public sealed class AcceptedAtRouteHttpResult : IResult
1017
{
1118
/// <summary>
12-
/// Initializes a new instance of the <see cref="AcceptedAtRouteResult"/> class with the values
19+
/// Initializes a new instance of the <see cref="AcceptedAtRouteHttpResult"/> class with the values
1320
/// provided.
1421
/// </summary>
1522
/// <param name="routeValues">The route data to use for generating the URL.</param>
1623
/// <param name="value">The value to format in the entity body.</param>
17-
public AcceptedAtRouteResult(object? routeValues, object? value)
24+
internal AcceptedAtRouteHttpResult(object? routeValues, object? value)
1825
: this(routeName: null, routeValues: routeValues, value: value)
1926
{
2027
}
2128

2229
/// <summary>
23-
/// Initializes a new instance of the <see cref="AcceptedAtRouteResult"/> class with the values
30+
/// Initializes a new instance of the <see cref="AcceptedAtRouteHttpResult"/> class with the values
2431
/// provided.
2532
/// </summary>
2633
/// <param name="routeName">The name of the route to use for generating the URL.</param>
2734
/// <param name="routeValues">The route data to use for generating the URL.</param>
2835
/// <param name="value">The value to format in the entity body.</param>
29-
public AcceptedAtRouteResult(
36+
internal AcceptedAtRouteHttpResult(
3037
string? routeName,
3138
object? routeValues,
3239
object? value)
33-
: base(value, StatusCodes.Status202Accepted)
3440
{
41+
Value = value;
3542
RouteName = routeName;
3643
RouteValues = new RouteValueDictionary(routeValues);
44+
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
3745
}
3846

47+
/// <summary>
48+
/// Gets the object result.
49+
/// </summary>
50+
public object? Value { get; }
51+
3952
/// <summary>
4053
/// Gets the name of the route to use for generating the URL.
4154
/// </summary>
@@ -46,12 +59,17 @@ public AcceptedAtRouteResult(
4659
/// </summary>
4760
public RouteValueDictionary RouteValues { get; }
4861

49-
/// <inheritdoc />
50-
protected override void ConfigureResponseHeaders(HttpContext context)
62+
/// <summary>
63+
/// Gets the HTTP status code.
64+
/// </summary>
65+
public int StatusCode => StatusCodes.Status202Accepted;
66+
67+
/// <inheritdoc/>
68+
public Task ExecuteAsync(HttpContext httpContext)
5169
{
52-
var linkGenerator = context.RequestServices.GetRequiredService<LinkGenerator>();
70+
var linkGenerator = httpContext.RequestServices.GetRequiredService<LinkGenerator>();
5371
var url = linkGenerator.GetUriByAddress(
54-
context,
72+
httpContext,
5573
RouteName,
5674
RouteValues,
5775
fragment: FragmentString.Empty);
@@ -61,6 +79,11 @@ protected override void ConfigureResponseHeaders(HttpContext context)
6179
throw new InvalidOperationException("No route matches the supplied values.");
6280
}
6381

64-
context.Response.Headers.Location = url;
82+
// Creating the logger with a string to preserve the category after the refactoring.
83+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
84+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.AcceptedAtRouteResult");
85+
86+
httpContext.Response.Headers.Location = url;
87+
return HttpResultsHelper.WriteResultAsJsonAsync(httpContext, logger, Value, StatusCode);
6588
}
6689
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Http;
5+
6+
using System.Threading.Tasks;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Logging;
9+
10+
/// <summary>
11+
/// An <see cref="IResult"/> that on execution will write an object to the response
12+
/// with status code Accepted (202) and Location header.
13+
/// Targets a registered route.
14+
/// </summary>
15+
public sealed class AcceptedHttpResult : IResult
16+
{
17+
/// <summary>
18+
/// Initializes a new instance of the <see cref="AcceptedHttpResult"/> class with the values
19+
/// provided.
20+
/// </summary>
21+
/// <param name="location">The location at which the status of requested content can be monitored.</param>
22+
/// <param name="value">The value to format in the entity body.</param>
23+
internal AcceptedHttpResult(string? location, object? value)
24+
{
25+
Value = value;
26+
Location = location;
27+
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
28+
}
29+
30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="AcceptedHttpResult"/> class with the values
32+
/// provided.
33+
/// </summary>
34+
/// <param name="locationUri">The location at which the status of requested content can be monitored.</param>
35+
/// <param name="value">The value to format in the entity body.</param>
36+
internal AcceptedHttpResult(Uri locationUri, object? value)
37+
{
38+
Value = value;
39+
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
40+
41+
if (locationUri == null)
42+
{
43+
throw new ArgumentNullException(nameof(locationUri));
44+
}
45+
46+
if (locationUri.IsAbsoluteUri)
47+
{
48+
Location = locationUri.AbsoluteUri;
49+
}
50+
else
51+
{
52+
Location = locationUri.GetComponents(UriComponents.SerializationInfoString, UriFormat.UriEscaped);
53+
}
54+
}
55+
56+
/// <summary>
57+
/// Gets the object result.
58+
/// </summary>
59+
public object? Value { get; }
60+
61+
/// <summary>
62+
/// Gets the HTTP status code.
63+
/// </summary>
64+
public int StatusCode => StatusCodes.Status202Accepted;
65+
66+
/// <summary>
67+
/// Gets the location at which the status of the requested content can be monitored.
68+
/// </summary>
69+
public string? Location { get; }
70+
71+
/// <inheritdoc/>
72+
public Task ExecuteAsync(HttpContext httpContext)
73+
{
74+
if (!string.IsNullOrEmpty(Location))
75+
{
76+
httpContext.Response.Headers.Location = Location;
77+
}
78+
79+
// Creating the logger with a string to preserve the category after the refactoring.
80+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
81+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.AcceptedResult");
82+
return HttpResultsHelper.WriteResultAsJsonAsync(
83+
httpContext,
84+
logger,
85+
Value,
86+
StatusCode);
87+
}
88+
}

src/Http/Http.Results/src/AcceptedResult.cs

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Microsoft.AspNetCore.Http;
5+
6+
using Microsoft.Extensions.DependencyInjection;
7+
using Microsoft.Extensions.Logging;
8+
9+
/// <summary>
10+
/// An <see cref="IResult"/> that on execution will write an object to the response
11+
/// with Bad Request (400) status code.
12+
/// </summary>
13+
public sealed class BadRequestObjectHttpResult : IResult
14+
{
15+
/// <summary>
16+
/// Initializes a new instance of the <see cref="BadRequestObjectHttpResult"/> class with the values
17+
/// provided.
18+
/// </summary>
19+
/// <param name="error">The error content to format in the entity body.</param>
20+
internal BadRequestObjectHttpResult(object? error)
21+
{
22+
Value = error;
23+
HttpResultsHelper.ApplyProblemDetailsDefaultsIfNeeded(Value, StatusCode);
24+
}
25+
26+
/// <summary>
27+
/// Gets the object result.
28+
/// </summary>
29+
public object? Value { get; internal init; }
30+
31+
/// <summary>
32+
/// Gets the HTTP status code.
33+
/// </summary>
34+
public int StatusCode => StatusCodes.Status400BadRequest;
35+
36+
/// <inheritdoc/>
37+
public Task ExecuteAsync(HttpContext httpContext)
38+
{
39+
// Creating the logger with a string to preserve the category after the refactoring.
40+
var loggerFactory = httpContext.RequestServices.GetRequiredService<ILoggerFactory>();
41+
var logger = loggerFactory.CreateLogger("Microsoft.AspNetCore.Http.Result.BadRequestObjectResult");
42+
43+
return HttpResultsHelper.WriteResultAsJsonAsync(
44+
httpContext,
45+
logger: logger,
46+
Value,
47+
StatusCode);
48+
}
49+
}

src/Http/Http.Results/src/BadRequestObjectResult.cs

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)