tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
ApiControllerBase.cs
Go to the documentation of this file.
1using System;
2using System.Net.Mime;
3using System.Threading;
4using System.Threading.Tasks;
5
6using Microsoft.AspNetCore.Mvc;
7using Microsoft.AspNetCore.Mvc.Filters;
8using Microsoft.Extensions.Primitives;
9using Microsoft.Net.Http.Headers;
10
12{
16 [Produces(MediaTypeNames.Application.Json)]
17 [ApiController]
18 public abstract class ApiControllerBase : Controller
19 {
21 public sealed override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
22 {
23 // never cache an API response
24 Response.Headers.Add(HeaderNames.CacheControl, new StringValues("no-cache"));
25
26 var errorCase = await HookExecuteAction(
27 () => base.OnActionExecutionAsync(context, next),
28 Request.HttpContext.RequestAborted);
29
30 if (errorCase != null)
31 await errorCase.ExecuteResultAsync(context);
32 }
33
40 protected virtual async ValueTask<IActionResult?> HookExecuteAction(Func<Task> executeAction, CancellationToken cancellationToken)
41 {
42 ArgumentNullException.ThrowIfNull(executeAction);
43
44 await executeAction();
45 return null;
46 }
47 }
48}
Base class for all API style controllers.
virtual async ValueTask< IActionResult?> HookExecuteAction(Func< Task > executeAction, CancellationToken cancellationToken)
Hook for executing a request.
override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)