generated from Avanade/avanade-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathExtendedStatusCodeResult.cs
50 lines (42 loc) · 1.89 KB
/
ExtendedStatusCodeResult.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) Avanade. Licensed under the MIT License. See https://github.com/Avanade/CoreEx
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Net;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
namespace CoreEx.AspNetCore.WebApis
{
/// <summary>
/// Represents an extended <see cref="StatusCodeResult"/> that enables customization of the <see cref="HttpResponse"/>.
/// </summary>
/// <param name="statusCode">The <see cref="HttpStatusCode"/>.</param>
public class ExtendedStatusCodeResult(HttpStatusCode statusCode) : StatusCodeResult((int)statusCode)
{
/// <summary>
/// Gets or sets the <see cref="Microsoft.AspNetCore.Http.Headers.ResponseHeaders.Location"/> <see cref="Uri"/>.
/// </summary>
public Uri? Location { get; set; }
/// <summary>
/// Gets or sets the function to perform the extended <see cref="HttpResponse"/> customization.
/// </summary>
[JsonIgnore]
public Func<HttpResponse, Task>? BeforeExtension { get; set; }
/// <summary>
/// Gets or sets the function to perform the extended <see cref="HttpResponse"/> customization.
/// </summary>
[JsonIgnore]
public Func<HttpResponse, Task>? AfterExtension { get; set; }
/// <inheritdoc/>
public override async Task ExecuteResultAsync(ActionContext context)
{
if (Location != null)
context.HttpContext.Response.GetTypedHeaders().Location = Location;
if (BeforeExtension != null)
await BeforeExtension(context.HttpContext.Response).ConfigureAwait(false);
await base.ExecuteResultAsync(context).ConfigureAwait(false);
if (AfterExtension != null)
await AfterExtension(context.HttpContext.Response).ConfigureAwait(false);
}
}
}