| | | 1 | | using Microsoft.AspNetCore.Authorization; |
| | | 2 | | using Microsoft.AspNetCore.Diagnostics; |
| | | 3 | | using Microsoft.AspNetCore.Mvc; |
| | | 4 | | using ProjectTemplate.Web.Constants; |
| | | 5 | | using ProjectTemplate.Web.Models; |
| | | 6 | | |
| | | 7 | | namespace ProjectTemplate.Web.Controllers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Controller for handling the home page and error routes. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <param name="logger">The logger instance for the controller.</param> |
| | 18 | 13 | | public partial class HomeController(ILogger<HomeController> logger) : Controller |
| | | 14 | | { |
| | 18 | 15 | | private readonly ILogger<HomeController> _logger = logger; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Displays an error page for the current request. If a status code is provided it will be used; |
| | | 19 | | /// otherwise a 500 Internal Server Error status code is assumed. The method logs either the |
| | | 20 | | /// unhandled exception or the status code routing information and returns an Error view |
| | | 21 | | /// containing an ErrorViewModel with the resolved status code and request id. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="statusCode">Optional HTTP status code to use for the response. If null, 500 is used.</param> |
| | | 24 | | /// <returns>An IActionResult that renders the Error view.</returns> |
| | | 25 | | [AllowAnonymous] |
| | | 26 | | [Route("Home/Error/{statusCode:int?}")] |
| | | 27 | | [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] |
| | | 28 | | public IActionResult Error(int? statusCode = null) |
| | | 29 | | { |
| | 18 | 30 | | int resolvedStatusCode = statusCode ?? StatusCodes.Status500InternalServerError; |
| | | 31 | | |
| | 18 | 32 | | Response.StatusCode = resolvedStatusCode; |
| | | 33 | | |
| | 18 | 34 | | IExceptionHandlerPathFeature? exceptionFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>(); |
| | 18 | 35 | | IStatusCodeReExecuteFeature? statusCodeFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>(); |
| | | 36 | | |
| | 18 | 37 | | string originalPath = |
| | 18 | 38 | | exceptionFeature?.Path ?? |
| | 18 | 39 | | statusCodeFeature?.OriginalPath ?? |
| | 18 | 40 | | HttpContext.Request.Path.ToString(); |
| | | 41 | | |
| | 18 | 42 | | string? remoteIpAddress = HttpContext.Connection.RemoteIpAddress?.ToString(); |
| | | 43 | | |
| | 18 | 44 | | string requestId = HttpContext.TraceIdentifier; |
| | | 45 | | |
| | 18 | 46 | | if (exceptionFeature?.Error is not null) |
| | | 47 | | { |
| | 0 | 48 | | LogUnhandledExceptionRoutedToErrorPage( |
| | 0 | 49 | | _logger, |
| | 0 | 50 | | exceptionFeature.Error, |
| | 0 | 51 | | resolvedStatusCode, |
| | 0 | 52 | | originalPath, |
| | 0 | 53 | | remoteIpAddress, |
| | 0 | 54 | | requestId); |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | 18 | 58 | | LogStatusCodePageRoutedToErrorPage( |
| | 18 | 59 | | _logger, |
| | 18 | 60 | | resolvedStatusCode, |
| | 18 | 61 | | originalPath, |
| | 18 | 62 | | remoteIpAddress, |
| | 18 | 63 | | requestId); |
| | | 64 | | } |
| | | 65 | | |
| | 18 | 66 | | return View(new ErrorViewModel |
| | 18 | 67 | | { |
| | 18 | 68 | | StatusCode = resolvedStatusCode, |
| | 18 | 69 | | RequestId = requestId |
| | 18 | 70 | | }); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | [LoggerMessage( |
| | | 74 | | EventId = ApplicationLogEventIds.UnhandledExceptionRoutedToErrorPage, |
| | | 75 | | Level = LogLevel.Error, |
| | | 76 | | Message = "Unhandled exception routed to error page. StatusCode: {StatusCode}; OriginalPath: {OriginalPath}; Rem |
| | | 77 | | private static partial void LogUnhandledExceptionRoutedToErrorPage( |
| | | 78 | | ILogger logger, |
| | | 79 | | Exception exception, |
| | | 80 | | int statusCode, |
| | | 81 | | string originalPath, |
| | | 82 | | string? remoteIpAddress, |
| | | 83 | | string traceIdentifier); |
| | | 84 | | |
| | | 85 | | [LoggerMessage( |
| | | 86 | | EventId = ApplicationLogEventIds.StatusCodePageRoutedToErrorPage, |
| | | 87 | | Level = LogLevel.Warning, |
| | | 88 | | Message = "Status code page routed to error page. StatusCode: {StatusCode}; OriginalPath: {OriginalPath}; Remote |
| | | 89 | | private static partial void LogStatusCodePageRoutedToErrorPage( |
| | | 90 | | ILogger logger, |
| | | 91 | | int statusCode, |
| | | 92 | | string originalPath, |
| | | 93 | | string? remoteIpAddress, |
| | | 94 | | string traceIdentifier); |
| | | 95 | | } |