Tooark.AspNetCore 4.3.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Tooark.AspNetCore --version 4.3.0
                    
NuGet\Install-Package Tooark.AspNetCore -Version 4.3.0
                    
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="Tooark.AspNetCore" Version="4.3.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Tooark.AspNetCore" Version="4.3.0" />
                    
Directory.Packages.props
<PackageReference Include="Tooark.AspNetCore" />
                    
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 Tooark.AspNetCore --version 4.3.0
                    
#r "nuget: Tooark.AspNetCore, 4.3.0"
                    
#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 Tooark.AspNetCore@4.3.0
                    
#: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=Tooark.AspNetCore&version=4.3.0
                    
Install as a Cake Addin
#tool nuget:?package=Tooark.AspNetCore&version=4.3.0
                    
Install as a Cake Tool

Tooark.AspNetCore

Library that concentrates the ASP.NET Core specific pieces of Tooark, keeping the general-purpose packages free of the runtime requirement. Packages whose purpose is to integrate with ASP.NET Core (Tooark.Dtos, Tooark.Observability, Tooark.Securities.OpenId) declare the shared framework themselves.

🌍 Languages: πŸ‡ΊπŸ‡Έ English (this file) Β· πŸ‡§πŸ‡· PortuguΓͺs

Contents

Overview

The Tooark.AspNetCore package provides:

  • extensions for ASP.NET Core types, today the ModelStateDictionary;
  • the place where the ASP.NET Core requirement was moved to, taking it out of the general-purpose packages;
  • integration with the Tooark.Extensions localizer, translating the validation error keys;
  • the intended home for the family's filters and middlewares.

Why the package exists. Microsoft.AspNetCore.App is not an ordinary NuGet dependency: declaring it makes the application require the ASP.NET Core runtime to be installed, and that requirement is contagious β€” it propagates to every package that references it, and to whoever references those.

Until v3 the requirement came from Tooark.Extensions, because of a single file. In practice, a worker service or a command-line tool that used Tooark.ValueObjects got Microsoft.AspNetCore.App in its own runtimeconfig.json and would not start on a mcr.microsoft.com/dotnet/runtime image.

From v4 on the requirement lives here. Web projects reference this package; the others do not pay for it.

Package Requires the ASP.NET Core runtime
Tooark.AspNetCore yes β€” it is its purpose
Tooark.Dtos yes β€” SearchDto, PaginationDto and ResponseDto use MVC and Http types
Tooark (aggregator) yes β€” by referencing the two above
Tooark.Extensions, Tooark.Utils, Tooark.ValueObjects, Tooark.Entities, Tooark.Attributes and the rest no

πŸ”§ Installation

dotnet add package Tooark.AspNetCore

The package does not require registration in the container: GetErrors is an extension method and works without configuration. When the filters and middlewares arrive, they will bring their own registration.

Runtime requirement: because it declares the shared framework, whoever consumes this package needs the ASP.NET Core runtime installed. In a Docker image, use mcr.microsoft.com/dotnet/aspnet instead of mcr.microsoft.com/dotnet/runtime. A web application already uses that image, so in practice nothing changes.


πŸ“¦ Components

ModelState extensions

  • ModelStateExtension.GetErrors(ModelStateDictionary): returns the ModelState error messages.

Walks every entry of the ModelStateDictionary and gathers the ErrorMessage of each error into a single list. A field with more than one error contributes all of its messages. The order is the ModelStateDictionary's, which enumerates by field key, not the order in which the errors were recorded.

Integration with the validations

The returned messages are what the Tooark.Attributes attributes produce: translation keys in the Key;Field format, such as Field.Required;Email. They become text through the IStringLocalizer of Tooark.Extensions, which resolves the language from the request's execution flow.

Errors generated by model binding itself β€” an integer receiving text, for instance β€” come with the framework's message, not with a key. The localizer returns that text unchanged and flags ResourceNotFound, which lets you tell the two cases apart when it matters.

Filters and middlewares

Not shipped yet. When they exist, they go in this package, under the Filters/ and Middlewares/ folders. The family only splits a package when the dependency profile differs β€” that is what motivated separating Tooark.Mediator.EntityFrameworkCore, which pulls Entity Framework Core, from Tooark.Mediator, which does not use it. Filters, middlewares and the extensions here share exactly the same profile, so splitting them into separate packages would reduce nothing.

Namespaces

The extensions live in Tooark.AspNetCore.Extensions.


πŸ“ Usage Examples

Reading the ModelState errors

using Microsoft.AspNetCore.Mvc;
using Tooark.AspNetCore.Extensions;

[ApiController]
[Route("people")]
public sealed class PersonController : ControllerBase
{
  [HttpPost]
  public IActionResult Create([FromBody] CreatePersonDto dto)
  {
    if (!ModelState.IsValid)
    {
      // ["Field.Invalid;Document", "Field.Invalid;Email"]
      var errors = ModelState.GetErrors();

      return BadRequest(errors);
    }

    return Ok();
  }
}

With translated messages

The keys become text through the IStringLocalizer, which comes from Tooark.Extensions and is registered there:

using Tooark.Extensions.Injections;

builder.Services.AddTooarkExtensions();
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Localization;
using Tooark.AspNetCore.Extensions;

[ApiController]
[Route("people")]
public sealed class PersonController(IStringLocalizer localizer) : ControllerBase
{
  [HttpPost]
  public IActionResult Create([FromBody] CreatePersonDto dto)
  {
    if (!ModelState.IsValid)
    {
      // ["The Document field is invalid", "The E-mail field is invalid"]
      var errors = ModelState.GetErrors().Select(error => localizer[error].Value);

      return BadRequest(errors);
    }

    return Ok();
  }
}

The DTO validated by the Tooark.Attributes attributes:

using Tooark.Attributes;

public sealed class CreatePersonDto
{
  [EmailValidation]
  public string Email { get; set; } = null!;

  [DocumentValidation("CPF")]
  public string Cpf { get; set; } = null!;
}

The name that appears in the message is the attribute's, not the property's: that is why the Cpf field produces Field.Invalid;Document, the default of DocumentValidationAttribute. To align the two, pass the propertyName:

[DocumentValidation("CPF", propertyName: "Cpf")]
public string Cpf { get; set; } = null!;

A filter to avoid repeating the check

The same block in every endpoint calls for a filter. While the package does not ship one, it fits in a few lines:

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Localization;
using Tooark.AspNetCore.Extensions;

public sealed class ValidationFilter(IStringLocalizer localizer) : IActionFilter
{
  public void OnActionExecuting(ActionExecutingContext context)
  {
    if (context.ModelState.IsValid)
    {
      return;
    }

    var errors = context.ModelState.GetErrors().Select(error => localizer[error].Value);

    context.Result = new BadRequestObjectResult(errors);
  }

  public void OnActionExecuted(ActionExecutedContext context) { }
}
builder.Services.AddControllers(options => options.Filters.Add<ValidationFilter>());

πŸ“‹ Dependencies

Package Version Description
Tooark.Extensions 4.x Error message localization
Microsoft.AspNetCore.App (shared framework) 8.x/10.x ModelStateDictionary

πŸͺͺ Contributing

Contributions are welcome! Feel free to open issues and pull requests in the Tooark.AspNetCore repository.

πŸ“„ License

This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  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 Tooark.AspNetCore:

Package Downloads
Tooark

Package with all Tooark resources for .NET applications.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.7.1 0 10/17/2026
4.7.0 108 9/30/2026
4.6.0 138 9/29/2026
4.5.0 109 9/29/2026
4.4.0 107 9/28/2026
4.3.0 112 9/26/2026
4.2.1 109 9/25/2026
4.2.0 128 9/20/2026
4.1.0 107 9/18/2026
4.0.1 138 8/25/2026
4.0.0 129 8/25/2026