Skip to content

Commit b107d77

Browse files
Use JsonResult
Use JsonResult instead of ObjectResult.
1 parent 7391ce3 commit b107d77

File tree

1 file changed

+9
-28
lines changed

1 file changed

+9
-28
lines changed

src/TodoApp/ApiModule.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static IEndpointRouteBuilder MapApiRoutes(this IEndpointRouteBuilder buil
3838
return new NotFoundResult();
3939
}
4040

41-
return new ObjectResult(model) as IResult;
41+
return new JsonResult(model) as IResult;
4242
}).RequireAuthorization();
4343

4444
// Create a new Todo item
@@ -92,47 +92,28 @@ public static IEndpointRouteBuilder MapApiRoutes(this IEndpointRouteBuilder buil
9292
return builder;
9393
}
9494

95-
// HACK Custom result types until ObjectResult implements IResult.
95+
// HACK Custom result types until CreatedAtResult/ObjectResult implements IResult.
9696
// See https://github.com/dotnet/aspnetcore/issues/32565.
9797

98-
private sealed class CreatedAtResult : ObjectResult
98+
private sealed class CreatedAtResult : IResult
9999
{
100100
private readonly string _location;
101+
private readonly object? _value;
101102

102103
internal CreatedAtResult(string location, object? value)
103-
: base(value)
104104
{
105105
_location = location;
106+
_value = value;
106107
}
107108

108-
public override async Task ExecuteAsync(HttpContext httpContext)
109-
{
110-
httpContext.Response.Headers.Location = _location;
111-
await base.ExecuteAsync(httpContext);
112-
}
113-
}
114-
115-
private class ObjectResult : IResult
116-
{
117-
public int? StatusCode { get; set; }
118-
119-
public object? Value { get; set; }
120-
121-
internal ObjectResult(object? value)
122-
{
123-
Value = value;
124-
}
125-
126-
public virtual async Task ExecuteAsync(HttpContext httpContext)
109+
public async Task ExecuteAsync(HttpContext httpContext)
127110
{
128111
var response = httpContext.Response;
129112

130-
if (StatusCode.HasValue)
131-
{
132-
response.StatusCode = StatusCode.Value;
133-
}
113+
response.Headers.Location = _location;
114+
response.StatusCode = StatusCodes.Status201Created;
134115

135-
await response.WriteAsJsonAsync(Value, httpContext.RequestAborted);
116+
await response.WriteAsJsonAsync(_value, httpContext.RequestAborted);
136117
}
137118
}
138119
}

0 commit comments

Comments
 (0)