πŸ“„ ErrorViewModel.cs Documentation


Table of Contents

  1. Overview & Purpose
  2. File Structure & Metadata
  3. Dependencies & Technologies
  4. Key Functional Areas
  5. UML Class Diagram
  6. Class: ErrorViewModel
  7. Usage in ASP.NET Core MVC
  8. Sample Code Blocks & Examples
  9. Integration Tips & Best Practices
  10. 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

  1. Data Carrier

    • Transfers a Request ID string from controller to view.
  2. Computed Property

    • ShowRequestId returns true if RequestId is neither null nor empty.
  3. 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 TraceIdentifier from the current HTTP context or other correlation ID.

ShowRequestId

  • Expression-bodied
    public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
    
  • Purpose
    • Quickly checks if RequestId has a valid value so that views can decide whether to render it.

7. Usage in ASP.NET Core MVC

  1. Controller Side

    • In your HomeController (or equivalent), you typically populate and return this model in an Error action:
    public IActionResult Error()
    {
        var model = new ErrorViewModel
        {
            RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier
        };
        return View(model);
    }
    
  2. 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 ShowRequestId and conditionally renders the ID for diagnostics.

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 like UseExceptionHandler("/Home/Error") in Startup.cs to funnel all exceptions into your Error action.
  • Correlate Logs
    Log RequestId on 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 extend ErrorViewModel with additional fields (e.g., ErrorMessage, StackTrace in 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!