Carter 9.0.0

dotnet add package Carter --version 9.0.0                
NuGet\Install-Package Carter -Version 9.0.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="Carter" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Carter --version 9.0.0                
#r "nuget: Carter, 9.0.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.
// Install Carter as a Cake Addin
#addin nuget:?package=Carter&version=9.0.0

// Install Carter as a Cake Tool
#tool nuget:?package=Carter&version=9.0.0                

Carter

Carter is a framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing the code to be more explicit and most importantly more enjoyable.

For a better understanding, take a good look at the samples inside this repo. The samples demonstrate usages of elegant extensions around common ASP.NET Core types as shown below.

Other extensions include:

  • Validate<T> / ValidateAsync<T> - FluentValidation extensions to validate incoming HTTP requests which is not available with ASP.NET Core Minimal APIs.
  • BindFile/BindFiles/BindFileAndSave/BindFilesAndSave - Allows you to easily get access to a file/files that has been uploaded. Alternatively you can call BindFilesAndSave and this will save it to a path you specify.
  • Routes to use in common ASP.NET Core middleware e.g., app.UseExceptionHandler("/errorhandler");.
  • IResponseNegotiators allow you to define how the response should look on a certain Accept header(content negotiation). Handling JSON is built in the default response but implementing an interface allows the user to choose how they want to represent resources.
  • All interface implementations for Carter components are registered into ASP.NET Core DI automatically. Implement the interface and off you go.

Releases

  • Latest NuGet Release NuGet Version
  • Latest NuGet Pre-Release NuGet Version
  • Lateset CI Release feedz.io
  • Build Status Build Status

Join our Slack Channel

Join our slack channel

Routing

Carter uses IEndpointRouteBuilder routing and all the extensions IEndpointConventionBuilder offers also known as Minimal APIs. For example you can define a route with authorization required like so:

app.MapGet("/", () => "There's no place like 127.0.0.1").RequireAuthorization();

Where does the name "Carter" come from?

I have been a huge fan of, and core contributor to Nancy, the best .NET web framework, for many years, and the name "Nancy" came about due to it being inspired from Sinatra the Ruby web framework. Frank Sinatra had a daughter called Nancy and so that's where it came from.

I was also trying to think of a derivative name, and I had recently listened to the song Empire State of Mind where Jay-Z declares he is the new Sinatra. His real name is Shaun Carter so I took Carter and here we are!

CI Builds

If you'd like to try the latest builds from the master branch add https://f.feedz.io/carter/carter/nuget/index.json to your NuGet.config and pick up the latest and greatest version of Carter.

Getting Started

You can get started using either the template or by adding the package manually to a new or existing application.

Template

https://www.nuget.org/packages/CarterTemplate/

  1. Install the template - dotnet new install CarterTemplate

  2. Create a new application using template - dotnet new carter -n MyCarterApp -o MyCarterApp

  3. Go into the new directory created for the application cd MyCarterApp

  4. Run the application - dotnet run

Package

https://www.nuget.org/packages/Carter

  1. Create a new empty ASP.NET Core application - dotnet new web -n MyCarterApp

  2. Change into the new project location - cd ./MyCarterApp

  3. Add Carter package - dotnet add package carter

  4. Modify your Program.cs to use Carter

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCarter();

var app = builder.Build();

app.MapCarter();
app.Run();
  1. Create a new Module
    public class HomeModule : ICarterModule
    {
        public void AddRoutes(IEndpointRouteBuilder app)
        {
            app.MapGet("/", () => "Hello from Carter!");
        }
    }
  1. Run the application - dotnet run

Sample

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<IActorProvider, ActorProvider>();
builder.Services.AddCarter();

var app = builder.Build();

app.MapCarter();
app.Run();

public class HomeModule : ICarterModule
{
    public void AddRoutes(IEndpointRouteBuilder app)
    {
        app.MapGet("/", () => "Hello from Carter!");
        app.MapGet("/qs", (HttpRequest req) =>
        {
            var ids = req.Query.AsMultiple<int>("ids");
            return $"It's {string.Join(",", ids)}";
        });
        app.MapGet("/conneg", (HttpResponse res) => res.Negotiate(new { Name = "Dave" }));
        app.MapPost("/validation", HandlePost);
    }

    private IResult HandlePost(HttpContext ctx, Person person, IDatabase database)
    {
        var result = ctx.Request.Validate(person);

        if (!result.IsValid)
        {
            return Results.UnprocessableEntity(result.GetFormattedErrors());
        }

        var id = database.StorePerson(person);

        ctx.Response.Headers.Location = $"/{id}";
        return Results.StatusCode(201);
    }
}

public record Person(string Name);

public interface IDatabase
{
    int StorePerson(Person person);
}

public class Database : IDatabase
{
    public int StorePerson(Person person)
    {
        //db stuff
    }
}

More samples

Configuration

As mentioned earlier Carter will scan for implementations in your app and register them for DI. However, if you want a more controlled app, Carter comes with a CarterConfigurator that allows you to register modules, validators and response negotiators manually.

Carter will use a response negotiator based on System.Text.Json, though it provides for custom implementations via the IResponseNegotiator interface. To use your own implementation of IResponseNegotiator (say, CustomResponseNegotiator), add the following line to the initial Carter configuration, in this case as part of Program.cs:


    builder.Services.AddCarter(configurator: c =>
    {
        c.WithResponseNegotiator<CustomResponseNegotiator>();
        c.WithModule<MyModule>();
        c.WithValidator<TestModelValidator>()
    });

Here again, Carter already ships with a response negotiator using Newtonsoft.Json, so you can wire up the Newtonsoft implementation with the following line:

    builder.Services.AddCarter(configurator: c =>
    {
        c.WithResponseNegotiator<NewtonsoftJsonResponseNegotiator>();
    });
Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (12)

Showing the top 5 NuGet packages that depend on Carter:

Package Downloads
Carter.ResponseNegotiators.Newtonsoft

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.

Linn.Common.Facade.Carter

Package Description

Carter.SirenNegotiator

A library to extend Carter to handle the siren hypermedia media type.

CleanTemplate

A Clean Architecture Base Template comprising all Baseic and Abstract and Contract types for initiating and structuring a .Net project.

Tnosc.Components.Abstractions.Api

Defining the abstractions in the Api layer

GitHub repositories (15)

Showing the top 5 popular GitHub repositories that depend on Carter:

Repository Stars
fullstackhero/dotnet-starter-kit
Production Grade Cloud-Ready .NET 8 Starter Kit (Web API + Blazor Client) with Multitenancy Support, and Clean/Modular Architecture that saves roughly 200+ Development Hours! All Batteries Included.
aspnetrun/run-aspnetcore-microservices
Microservices on .NET platforms used ASP.NET Web API, Docker, RabbitMQ, MassTransit, Grpc, Yarp API Gateway, PostgreSQL, Redis, SQLite, SqlServer, Marten, Entity Framework Core, CQRS, MediatR, DDD, Vertical and Clean Architecture implementation with using latest features of .NET 8 and C# 12
CarterCommunity/Carter
Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
CodeMazeBlog/CodeMazeGuides
The main repository for all the Code Maze guides
aliostad/CacheCow
An implementation of HTTP Caching in .NET Core and 4.5.2+ for both the client and the server
Version Downloads Last updated
9.0.0 7,344 11/16/2024
8.2.1 163,787 6/6/2024
8.2.0 19,632 5/24/2024
8.1.0 54,832 5/11/2024
8.0.0 236,308 12/4/2023
7.2.0 79,561 9/21/2023
7.1.0 113,445 4/22/2023
7.0.1 15,767 3/15/2023
7.0.0 33,220 11/21/2022
7.0.0-beta 189 11/21/2022
7.0.0-alpha1 159 11/18/2022
6.1.1 134,758 7/20/2022
6.1.0 2,510 7/15/2022
6.0.0 121,891 11/22/2021
6.0.0-rc4 1,259 11/12/2021
6.0.0-rc3 437 11/2/2021
6.0.0-rc2 343 10/25/2021
6.0.0-pre2 1,600 8/13/2021
5.2.0 145,736 9/6/2020
5.1.0 64,849 1/27/2020
5.0.0 4,576 11/25/2019
4.2.0 22,876 5/28/2019
4.1.0 5,934 3/6/2019
4.0.0 1,904 3/5/2019
3.11.0 5,621 12/31/2018
3.10.0 5,030 12/31/2018
3.8.0 5,554 10/17/2018
3.7.0 12,438 5/31/2018
3.6.0 5,018 4/12/2018
3.5.0 3,430 4/7/2018