using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;
namespace PowderCoating.Web.Controllers;
///
/// Handles the application root URL and static informational pages
/// (Privacy, Terms of Service, SLA, DPA, Security, Accessibility).
/// Also provides the global error handler endpoint used by the exception
/// handling middleware pipeline.
///
public class HomeController : Controller
{
private readonly ILogger _logger;
public HomeController(ILogger logger)
{
_logger = logger;
}
///
/// Handles requests to the application root (/). Authenticated users are
/// immediately redirected to the Dashboard; unauthenticated visitors are sent to
/// the Identity login page. This ensures no content is ever rendered at the root
/// URL — the root is purely a routing decision point.
///
public IActionResult Index()
{
if (User.Identity?.IsAuthenticated == true)
{
return RedirectToAction("Index", "Dashboard");
}
// Otherwise redirect to the login page
return Redirect("/Identity/Account/Login");
}
/// Renders the Privacy Policy static page.
public IActionResult Privacy()
{
return View();
}
/// Renders the Terms of Service static page.
public IActionResult TermsOfService()
{
return View();
}
/// Renders the Service Level Agreement static page.
public IActionResult ServiceLevelAgreement()
{
return View();
}
/// Renders the Data Processing Addendum (DPA) static page.
public IActionResult DataProcessingAddendum()
{
return View();
}
/// Renders the Security overview static page.
public IActionResult Security()
{
return View();
}
/// Renders the Accessibility statement static page.
public IActionResult Accessibility()
{
return View();
}
///
/// Global error handler endpoint — invoked by the ASP.NET Core exception handling
/// middleware when an unhandled exception propagates out of any controller.
/// Logs the exception with path, user identity, and trace Id for structured
/// log correlation, then renders a user-friendly error view.
/// Response caching is disabled (NoStore) to prevent error pages from
/// being cached and served to subsequent requests without a real error.
///
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
// Get the exception details from the exception handler
var exceptionHandlerPathFeature = HttpContext.Features.Get();
if (exceptionHandlerPathFeature?.Error != null)
{
var exception = exceptionHandlerPathFeature.Error;
var path = exceptionHandlerPathFeature.Path;
_logger.LogError(exception,
"Unhandled exception occurred. Path: {Path}, User: {User}, TraceId: {TraceId}",
path,
User.Identity?.Name ?? "Anonymous",
HttpContext.TraceIdentifier);
}
return View();
}
}