Skip to main content

HomeController

The HomeController is the main controller responsible for handling profile routing, language switching, and error pages. Namespace: dev_showcase.Controllers Base Class: Microsoft.AspNetCore.Mvc.Controller

Profile Action

Handles profile page requests for different developer profiles.
public IActionResult Profile(string profile)
profile
string
required
The profile identifier. Must be one of the valid profile values.

Valid Profile Values

The controller validates profile names against a case-insensitive whitelist:
  • dataScience - Data Science profile
  • webDev - Web Development profile
  • dataAnalyst - Data Analyst profile
  • DataAnalysis - Data Analysis profile

Returns

  • IActionResult: Returns View("HomePage") with the profile set in ViewData["Profile"] if valid
  • NotFoundResult: Returns 404 if the profile is not in the valid profiles list

Route Mapping

This action is mapped through two route patterns defined in Program.cs:
  1. With Language: /{lang}/{profile} - e.g., /en/dataScience
  2. Without Language: /{profile} - e.g., /dataScience
Both routes include constraints to ensure valid profile values.

SetLanguage Action

Sets the user’s language preference via a cookie.
[HttpPost]
public IActionResult SetLanguage(string lang)
lang
string
The language code to set. Defaults to “es” if not provided or null.
The method sets a cookie named lang with the following options:
  • Expires: 1 year from the current UTC time
  • IsEssential: true - cookie is essential for the application to function
  • Default Value: “es” (Spanish) if lang is null or not provided

Returns

  • OkResult: Returns HTTP 200 status code

HTTP Method

POST - This endpoint only accepts POST requests

Error Action

Displays the error page with diagnostic information.
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()

Response Caching

The error action is decorated with caching attributes to prevent caching:
  • Duration: 0 seconds
  • Location: ResponseCacheLocation.None
  • NoStore: true

Returns

  • ViewResult: Returns the Error view with an ErrorViewModel instance
  • The RequestId is populated with either:
    • Activity.Current?.Id if available
    • HttpContext.TraceIdentifier as fallback

Route

In production environments, this is configured as the exception handler route: /Home/Error

Build docs developers (and LLMs) love