public async Task OnExceptionAsync(ExceptionContext context) { //if (!hostingEnvironment.IsDevelopment()) { return; } var response = new ErrorResponseModel { Message = "An unexpected error occurred.", StatusCode = HttpStatusCode.InternalServerError }; memoryCache.Set($"error-uid:{response.UId}", response.ToJsonString()); var accept = context.HttpContext.Request.Headers["Accept"]; var contentType = context.HttpContext.Request.Headers["Content-Type"]; if (contentType.Any(a => a == "application/json") || !accept.Any(a => a.Contains("text/html"))) { context.HttpContext.Response.ContentType = "application/json"; context.HttpContext.Response.StatusCode = (int)response.StatusCode; await context.HttpContext.Response.WriteAsync(response.ToJsonString()); return; } context.Result = new RedirectToActionResult("Error", "Home", new { uid = response.UId }); }
private async Task HandleExceptionAsync(HttpContext context, Exception ex) { var response = new ErrorResponseModel(); if (ex is HttpException httpException) { response.StatusCode = httpException.StatusCode; response.Message = httpException.Message; } context.Response.ContentType = "application/json"; context.Response.StatusCode = (int)response.StatusCode; await context.Response.WriteAsync(response.ToJsonString()); }