Scott Hanselman

The open source Carter Community Project adds opinionated elegance to ASP.NET Core routing

November 27, 2019 Comment on this post [6] Posted in ASP.NET | Open Source
Sponsored By

imageI 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.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
November 29, 2019 13:54
Carter author here, thanks for this great blog post Scott, we appreciate it!

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
Get<GetActors>("/", ctx =>.....)


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,

Get("/", async (req, res) => await res.WriteAsync("Hello from Carter!")).RequiresAuthorization().RequiresHost("foobar.com");


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
November 29, 2019 18:27
So... Carter is basically Express.js for .NET?
November 29, 2019 18:35
Mertlin4 Yeah, I thought the same. But I like to have the option.
November 04, 2020 19:43
Amazing tһings hегe. I'm ѵery hаppy to pder yoᥙr post.
Tһanks a lot and I'm looking ahead tо contact you.
Will yoս kindly drop mе a e-mail? https://www.sportsspread.com/
November 05, 2020 13:01
I do consider all of the ideas you have presented in your post.
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.
November 05, 2020 21:11
Silahkan dicoba..situs slot online terpercaya 2020%ED%A0%BD%ED%B1%8D%ED%A0%BD%ED%B1%8D%ED%A0%BD%ED%B1%8DSitus
:http://bit.ly/336-aw8indoWA :http://bit.ly/336WA-aw8indo

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.