Skip to main content

ErrorViewModel

The ErrorViewModel is used to pass error information to the error view page. Namespace: dev_showcase.Models

Properties

RequestId

public string? RequestId { get; set; }
RequestId
string?
A unique identifier for the current request. Nullable string type.
The RequestId is set to either:
  • Activity.Current?.Id - The current activity’s trace ID if available
  • HttpContext.TraceIdentifier - The HTTP context trace identifier as a fallback
This ID is useful for correlating errors with log entries and diagnostic traces.

ShowRequestId

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
ShowRequestId
bool
A computed property that returns true if RequestId is not null or empty.
This read-only property is used in the error view to conditionally display the request ID.

Usage in Error Handling

The ErrorViewModel is instantiated in the HomeController.Error() action method:
public IActionResult Error()
{
    return View(new ErrorViewModel 
    { 
        RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier 
    });
}

Purpose

This model serves two main purposes:
  1. Error Tracking: Provides a unique identifier to track and debug specific error instances
  2. User Display: Allows the error view to conditionally show diagnostic information based on whether a request ID is available

Production Configuration

In production environments (non-Development), the application is configured to redirect errors to the error handler:
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
}
This ensures that unhandled exceptions are gracefully handled and presented to users with the appropriate request tracking information.

Build docs developers (and LLMs) love