π ErrorViewModel.cs Documentation
Table of Contents
- Overview & Purpose
- File Structure & Metadata
- Dependencies & Technologies
- Key Functional Areas
- UML Class Diagram
- Class: ErrorViewModel
- Usage in ASP.NET Core MVC
- Sample Code Blocks & Examples
- Integration Tips & Best Practices
- Conclusion
1. Overview & Purpose
ErrorViewModel.cs is a simple ViewModel class within the IrbyImaging.Models namespace.
It is designed to carry error-related data (specifically a request identifier) from the server-side ASP.NET Core application to the client-side error page (Razor view).
π Use Case
- Displaying a unique Request ID on error pages to help with logging, diagnostics, and customer support.
2. File Structure & Metadata
IrbyImaging/Models/ErrorViewModel.cs
βββ Namespace: IrbyImaging.Models
βββ Class: ErrorViewModel
File Header Comments
// ***********************************************************************
// Assembly : IrbyImaging.UI
// Author : kvamshi
// Created : 01-11-2022
// Last Modified By : kvamshi
// Last Modified On : 01-11-2022
// <copyright file="ErrorViewModel.cs" company="IrbyImaging.UI">
// Copyright (c) . All rights reserved.
// </copyright>
// ***********************************************************************
3. Dependencies & Technologies
| Dependency | Purpose |
|---|---|
System |
Provides base classes and fundamental types |
| ASP.NET Core MVC | Hosts the ViewModel to the Razor view |
No external NuGet packages are required.
4. Key Functional Areas
-
Data Carrier
- Transfers a Request ID string from controller to view.
-
Computed Property
ShowRequestIdreturnstrueifRequestIdis neither null nor empty.
-
Error Diagnostics
- Enables end-user support by surfacing a unique ID to correlate logs.
5. UML Class Diagram
classDiagram
class ErrorViewModel {
<<ViewModel>>
- string RequestId
+ bool ShowRequestId {get;}
}
6. Class: ErrorViewModel
6.1 Properties
| Property | Type | Access | Description |
|---|---|---|---|
RequestId |
string |
get; set; |
Holds the unique identifier of the current HTTP request. |
ShowRequestId |
bool |
get (expr) |
Indicates whether RequestId is not null or empty. |
RequestId
- Getter / Setter
- Standard property.
- Purpose
- Captures the
TraceIdentifierfrom the current HTTP context or other correlation ID.
- Captures the
ShowRequestId
- Expression-bodied
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); - Purpose
- Quickly checks if
RequestIdhas a valid value so that views can decide whether to render it.
- Quickly checks if
7. Usage in ASP.NET Core MVC
-
Controller Side
- In your
HomeController(or equivalent), you typically populate and return this model in anErroraction:
public IActionResult Error() { var model = new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier }; return View(model); } - In your
-
Razor View (
Error.cshtml)@model IrbyImaging.Models.ErrorViewModel <h1 class="text-danger">Oops! An error occurred.</h1> @if (Model.ShowRequestId) { <p> <strong>Request ID:</strong> @Model.RequestId </p> }- The view checks
ShowRequestIdand conditionally renders the ID for diagnostics.
- The view checks
8. Sample Code Blocks & Examples
8.1 Creating & Using ErrorViewModel
using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using IrbyImaging.Models;
public class HomeController : Controller
{
public IActionResult Error()
{
// Obtain the current trace identifier
string traceId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
var errorModel = new ErrorViewModel
{
RequestId = traceId
};
return View(errorModel);
}
}
8.2 Razor View Snippet
@model IrbyImaging.Models.ErrorViewModel
@{
ViewData["Title"] = "Error";
}
<h2>Error</h2>
<p>Sorry, something went wrong! π</p>
@if (Model.ShowRequestId)
{
<div class="alert alert-info">
π <strong>Request ID:</strong> @Model.RequestId
</div>
}
9. Integration Tips & Best Practices
- Centralize Error Handling
Use middleware likeUseExceptionHandler("/Home/Error")in Startup.cs to funnel all exceptions into yourErroraction. - Correlate Logs
LogRequestIdon server-side exceptions; display it to the user to help support trace the issue. - Security
Donβt expose sensitive dataβonly display a non-guessable, random trace identifier. - Customization
You can extendErrorViewModelwith additional fields (e.g.,ErrorMessage,StackTracein Development mode).
10. Conclusion
The ErrorViewModel is a lightweight but crucial component in the error-reporting pipeline of an ASP.NET Core MVC application. It elegantly encapsulates a traceable identifier and provides a handy computed property to streamline Razor view logic. By exposing just whatβs necessary (the Request ID), it enhances diagnostics while maintaining simplicity and separation of concerns.
β¨ Thank you for reading! Should you have any questions or enhancements, feel free to reach out!