The open source Carter Community Project adds opinionated elegance to ASP.NET Core routing
I blogged about NancyFX 6 years ago and since then lots of ASP.NET open source frameworks that build upon - and improve! - web development on .NET have become popular.
There's more than one way to serve and angle bracket (or curly brace) my friends!
Jonathan Channon and the Carter Community (JC was a core Nancy contributor as well) have been making a thin layer of extension methods and conventions on top of ASP.NET Core to make URL routing "more elegant." Carter adds and formalizes a more opinionated framework and also adds direct support for the amazing FluentValidation.
One of the best things about ASP.NET Core is its extensibility model and Carter takes full advantage of that. Carter is ASP.NET.
You can add Carter to your existing ASP.NET Core app by just "dotnet add package carter
" and adding it to your Startup.cs:
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddCarter();
}
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(builder => builder.MapCarter());
}
}
At this point you can make a quick "microservice" - in this case just handle an HTTP GET - in almost no code, and it's super clear to read:
public class HomeModule : CarterModule
{
public HomeModule()
{
Get("/", async (req, res) => await res.WriteAsync("Hello from Carter!"));
}
}
Or you can add Carter as a template so you can later "dotnet new carter
." Start by adding the Carter Template with "dotnet new -i CarterTemplate
" and now you can make a new boilerplate starter app anytime.
There's a lot of great sample code on the Carter Community GitHub. Head over to https://github.com/CarterCommunity/Carter/tree/master/samples and give them more Stars!
Carter can also cleanly integrate with your existing ASP.NET apps because, again, it's extensions and improvements on top of ASP.NET. Now how you can add Carter to a ASP.NET Core app that's using Controllers in the MVC pattern just like this:
public void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseEndpoints(builder =>
{
builder.MapDefaultControllerRoute();
builder.MapCarter();
});
}
Then easily handle a GET by returning a list of things as JSON like this:
this.Get<GetActors>("/actors", async (req, res) =>
{
var people = actorProvider.Get();
await res.AsJson(people);
});
Again, check out Carter on GitHub at and follow https://twitter.com/CarterLibs on Twitter!
Sponsor: Like C#? We do too! That’s why we've developed a fast, smart, cross-platform .NET IDE which gives you even more coding power. Clever code analysis, rich code completion, instant search and navigation, an advanced debugger... With JetBrains Rider, everything you need is at your fingertips. Code C# at the speed of thought on Linux, Mac, or Windows. Try JetBrains Rider today!
About Scott
Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.
About Newsletter
Tһanks a lot and I'm looking ahead tо contact you.
Will yoս kindly drop mе a e-mail? https://www.sportsspread.com/
They're really convincing and can definitely
work. Still, the posts are very quick for starters.
May you please prolong them a little from next time?
Thanks for the post.
:http://bit.ly/336-aw8indoWA :http://bit.ly/336WA-aw8indo
Comments are closed.
As you can see from Scott's image, Carter supports OpenApi out of the box, just make a call to /openapi and you'll get the JSON response. Obviously if you hook up SwaggerUI or any other templating tool you can see the metadata that you have defined by using the Carter generic API
As Scott stated, Carter is just extension methods over ASP.NET Core and as of Carter v5.0.0 it uses the APIs directly so you can use methods provided by ASP.NET Core such as route level authorization or host specific checks, for example,
Carter is also very fast, more so than MVC. Here's the Techempower results from the previous run. These need to be updated to the latest Carter version which hopefully will improve performance even more!
We also want to get everybody and anybody to get involved with Carter and we have some "Up for Grabs" issues that people can get stuck into, these are aimed at dipping your toe into Carter so you're not expected to know everything inside out. Please update the README or get stuck in as much as you want.
You can also come and just hang out in our Slack channel and say hi or ask any questions - Carter Slack Channel