Mjolnir.Extensions.Exceptions 10.0.6

dotnet add package Mjolnir.Extensions.Exceptions --version 10.0.6
                    
NuGet\Install-Package Mjolnir.Extensions.Exceptions -Version 10.0.6
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mjolnir.Extensions.Exceptions" Version="10.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mjolnir.Extensions.Exceptions" Version="10.0.6" />
                    
Directory.Packages.props
<PackageReference Include="Mjolnir.Extensions.Exceptions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mjolnir.Extensions.Exceptions --version 10.0.6
                    
#r "nuget: Mjolnir.Extensions.Exceptions, 10.0.6"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mjolnir.Extensions.Exceptions@10.0.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mjolnir.Extensions.Exceptions&version=10.0.6
                    
Install as a Cake Addin
#tool nuget:?package=Mjolnir.Extensions.Exceptions&version=10.0.6
                    
Install as a Cake Tool

Mjolnir.Extensions.Exceptions

Simple MCP server compatible exceptions, HTTP request validators, and global handlers for ASP.NET Core.

Overview

Mjolnir.Extensions.Exceptions provides a standardized way to handle exceptions and validation in ASP.NET Core applications. It includes base exception classes, global exception handlers that convert exceptions to ProblemDetails, and a fluent validation rule system for Minimal APIs.

Key Features:

  • MjolnirException: A base exception class with extensive static helper methods for common validation checks (e.g., ThrowIfNull, ThrowIfZero, ThrowIfNegative).
  • ValidationException: A specialized exception for accumulating and reporting multiple validation errors.
  • Global Handlers: Middleware handlers for MjolnirException and ValidationException that automatically produce standardized ProblemDetails responses.
  • RuleSet & Minimal API Integration: A fluent API for defining custom validation rules on endpoints.

Requirements

  • Runtime: .NET 10.0+
  • Language: C# 14.0+
  • Dependencies:
    • Microsoft.AspNetCore.OpenApi (10.0.5)
    • Microsoft.Extensions.DependencyInjection.Abstractions (10.0.5)
    • ModelContextProtocol.Core (1.1.0)

Setup

Installation

Install the package via NuGet:

dotnet add package Mjolnir.Extensions.Exceptions

Configuration

Register the exception handlers in your Program.cs:

using Mjolnir.Extensions.Exceptions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

// Add Mjolnir exception handlers
builder.Services.AddMjolnirExceptionsHandler();

var app = builder.Build();

// Use the exception handler middleware
app.UseMjolnirExceptionHandler();

app.Run();

Usage

MjolnirException

Use the static helper methods to perform validation and throw exceptions with specific HTTP status codes:

MjolnirException.ThrowIfNull(user, "User not found", HttpStatusCode.NotFound);
MjolnirException.ThrowIfNegative(price, "Price cannot be negative");

ValidationException

Accumulate multiple validation errors:

var ex = new ValidationException();

if (string.IsNullOrEmpty(request.Name))
    ex.WithError(nameof(request.Name), "Name is required");

if (request.Age < 18)
    ex.WithError(nameof(request.Age), "Must be at least 18");

ex.ThrowIfHasError();

Minimal API RuleSets

Define custom validation rules for your endpoints:

app.MapPost("/users", (User user) => Results.Ok(user))
   .AddRules<User>(rules =>
   {
       rules.AddRule(u => !string.IsNullOrEmpty(u.Username), "Username is required", nameof(User.Username));
       rules.AddRule(async u => await IsUsernameUnique(u.Username), "Username already taken", nameof(User.Username));
   });

Project Structure

Mjolnir.Extensions.Exceptions/
├── DependencyInjection/
│   └── Di.cs                       # DI registration extensions
├── Handlers/
│   ├── MjolnirExceptionHandler.cs   # Global handler for MjolnirException
│   └── ValidationExceptionHandler.cs # Global handler for ValidationException
├── Rules/
│   ├── RuleSet.cs                   # Fluent validation rule builder
│   └── RulesExtensions.cs           # Endpoint filter extensions for Rules
├── MjolnirException.cs              # Base exception with helper methods
├── ValidationException.cs           # Multi-error validation exception
└── Mjolnir.Extensions.Exceptions.csproj

License

This project is licensed under the LICENSE file in the root of the repository.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Mjolnir.Extensions.Exceptions:

Package Downloads
Mjolnir.Extensions.AspNetCore.Filtering

Simple entity filtering

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.6 140 4/9/2026
10.0.5 140 3/25/2026