< Summary

Information
Class: ProjectTemplate.Web.Controllers.HomeController
Assembly: ProjectTemplate.Web
File(s): /home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Controllers/HomeController.cs
Line coverage
77%
Covered lines: 24
Uncovered lines: 7
Coverable lines: 31
Total lines: 95
Line coverage: 77.4%
Branch coverage
50%
Covered branches: 7
Total branches: 14
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Error(...)50%171475.86%

File(s)

/home/runner/work/NetCoreApplicationTemplate/NetCoreApplicationTemplate/src/ProjectTemplate.Web/Controllers/HomeController.cs

#LineLine coverage
 1using Microsoft.AspNetCore.Authorization;
 2using Microsoft.AspNetCore.Diagnostics;
 3using Microsoft.AspNetCore.Mvc;
 4using ProjectTemplate.Web.Constants;
 5using ProjectTemplate.Web.Models;
 6
 7namespace 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>
 1813public partial class HomeController(ILogger<HomeController> logger) : Controller
 14{
 1815    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    {
 1830        int resolvedStatusCode = statusCode ?? StatusCodes.Status500InternalServerError;
 31
 1832        Response.StatusCode = resolvedStatusCode;
 33
 1834        IExceptionHandlerPathFeature? exceptionFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
 1835        IStatusCodeReExecuteFeature? statusCodeFeature = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
 36
 1837        string originalPath =
 1838            exceptionFeature?.Path ??
 1839            statusCodeFeature?.OriginalPath ??
 1840            HttpContext.Request.Path.ToString();
 41
 1842        string? remoteIpAddress = HttpContext.Connection.RemoteIpAddress?.ToString();
 43
 1844        string requestId = HttpContext.TraceIdentifier;
 45
 1846        if (exceptionFeature?.Error is not null)
 47        {
 048            LogUnhandledExceptionRoutedToErrorPage(
 049                _logger,
 050                exceptionFeature.Error,
 051                resolvedStatusCode,
 052                originalPath,
 053                remoteIpAddress,
 054                requestId);
 55        }
 56        else
 57        {
 1858            LogStatusCodePageRoutedToErrorPage(
 1859                _logger,
 1860                resolvedStatusCode,
 1861                originalPath,
 1862                remoteIpAddress,
 1863                requestId);
 64        }
 65
 1866        return View(new ErrorViewModel
 1867        {
 1868            StatusCode = resolvedStatusCode,
 1869            RequestId = requestId
 1870        });
 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}