public void OnResultExecuting(ResultExecutingContext context) { switch (context.Result) { case StatusCodeResult result: { var status = (HttpStatusCode)result.StatusCode; context.Result = ResultUtilities.Status(status); break; } case ObjectResult result: { var status = (HttpStatusCode)(result.StatusCode ?? 200); var type = result.Value?.GetType(); // ensure something is returned, instead of an empty response if (type == null) { context.Result = ResultUtilities.Status(status); } // wrap string responses else if (type == typeof(string)) { context.Result = ResultUtilities.Status(status, (string)result.Value); } break; } } }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { var control = context.HttpContext.RequestServices.GetService <IWriteControl>(); var environment = context.HttpContext.RequestServices.GetService <IHostEnvironment>(); try { await using (await control.EnterAsync()) await next(); } catch (WriteControlException e) { context.Result = ResultUtilities.Status(HttpStatusCode.ServiceUnavailable, e.ToStringWithTrace("Could not complete this request at the moment due to maintenance.", environment.IsProduction())); } }