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 callBindFilesAndSaveand this will save it to a path you specify.MapPost<T>/MapPut<T>- Allows Carter to validateTand if it fails it returns a 422 Problem Details response.MapFormPost<T>- Allows Carter to model bindTwhen submitting a form to the route.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.- Routes to use in common ASP.NET Core middleware e.g.,
app.UseExceptionHandler("/errorhandler");. - All interface implementations for Carter components are registered into ASP.NET Core DI automatically. Implement the interface and off you go.
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();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!
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.
You can get started using either the template or by adding the package manually to a new or existing application.
https://www.nuget.org/packages/CarterTemplate/
Install the template -
dotnet new install CarterTemplateCreate a new application using template -
dotnet new carter -n MyCarterApp -o MyCarterAppGo into the new directory created for the application
cd MyCarterAppRun the application -
dotnet run
https://www.nuget.org/packages/Carter
Create a new empty ASP.NET Core application -
dotnet new web -n MyCarterAppChange into the new project location -
cd ./MyCarterAppAdd Carter package -
dotnet add package carterModify your Program.cs to use Carter
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddCarter();varapp=builder.Build();app.MapCarter();app.Run();- Create a new Module
publicclassHomeModule:ICarterModule{publicvoidAddRoutes(IEndpointRouteBuilderapp){app.MapGet("/",()=>"Hello from Carter!");}}- Run the application -
dotnet run
varbuilder=WebApplication.CreateBuilder(args);builder.Services.AddSingleton<IActorProvider,ActorProvider>();builder.Services.AddCarter();varapp=builder.Build();app.MapCarter();app.Run();publicclassHomeModule:ICarterModule{publicvoidAddRoutes(IEndpointRouteBuilderapp){app.MapGet("/",()=>"Hello from Carter!");app.MapGet("/qs",(HttpRequestreq)=>{varids=req.Query.AsMultiple<int>("ids");return$"It's {string.Join(",",ids)}";});app.MapGet("/conneg",(HttpResponseres)=>res.Negotiate(new{Name="Dave"}));app.MapPost("/validation",HandlePost);app.MapFormPost("/formpost",(Personmodel)=>TypedResults.Ok(model)).DisableAntiforgery();}privateIResultHandlePost(HttpContextctx,Personperson,IDatabasedatabase){varresult=ctx.Request.Validate(person);if(!result.IsValid){returnResults.UnprocessableEntity(result.GetFormattedErrors());}varid=database.StorePerson(person);ctx.Response.Headers.Location=$"/{id}";returnResults.StatusCode(201);}}publicrecordPerson(stringName);publicinterfaceIDatabase{intStorePerson(Personperson);}publicclassDatabase:IDatabase{publicintStorePerson(Personperson){//db stuff}}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 and configure validator lifetimes.
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>();c.WithDefaultValidatorLifetime(ServiceLifetime.Singleton);c.WithValidatorServiceLifetimeFactory(t =>{iftisPersonValidator...})});If you wish to use Newtonsoft.Json Carter already ships a response negotiator in the package Carter.ResponseNegotiators.Newtonsoft. Once installed, it will automatically pick it up with no registration needed.
