Skip to main content

ErrorViewModel

The ErrorViewModel is used to display error information in the error view, including request tracking details.

Source

Models/ErrorViewModel.cs
namespace MvcCoreUtilidades.Models
{
    public class ErrorViewModel
    {
        public string? RequestId { get; set; }

        public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
    }
}

Properties

RequestId

RequestId
string?
The unique identifier for the current request, used for error tracking and diagnostics
Type: string? (nullable) Usage:
var model = new ErrorViewModel 
{ 
    RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier 
};

ShowRequestId

ShowRequestId
bool
Computed property that indicates whether the RequestId should be displayed
Type: bool (read-only) Behavior: Returns true if RequestId is not null or empty, false otherwise Usage in View:
Views/Shared/Error.cshtml
@model ErrorViewModel

@if (Model.ShowRequestId)
{
    <p>
        <strong>Request ID:</strong> @Model.RequestId
    </p>
}

Usage Example

In Controller

Controllers/HomeController.cs
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
public IActionResult Error()
{
    return View(new ErrorViewModel 
    { 
        RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier 
    });
}

In Error View

Views/Shared/Error.cshtml
@model ErrorViewModel
@{
    ViewData["Title"] = "Error";
}

<h1 class="text-danger">Error.</h1>
<h2 class="text-danger">An error occurred while processing your request.</h2>

@if (Model.ShowRequestId)
{
    <p>
        <strong>Request ID:</strong> <code>@Model.RequestId</code>
    </p>
}

<h3>Development Mode</h3>
<p>
    Swapping to <strong>Development</strong> environment will display more detailed 
    information about the error that occurred.
</p>

Request ID Sources

The RequestId is populated from two possible sources:
  1. Activity.Current?.Id - Distributed tracing identifier from the current activity
  2. HttpContext.TraceIdentifier - Fallback request identifier from ASP.NET Core
This ensures every error can be traced back to a specific request for debugging purposes.

HomeController

See Error action implementation

Error Handling

Learn about error handling in ASP.NET Core

Build docs developers (and LLMs) love