Scott Hanselman

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

November 27, '19 Comments [3] 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
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Friday, November 29, 2019 9:54:38 AM UTC
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
Friday, November 29, 2019 2:27:11 PM UTC
So... Carter is basically Express.js for .NET?
Merlin4
Friday, November 29, 2019 2:35:38 PM UTC
Mertlin4 Yeah, I thought the same. But I like to have the option.
Name
E-mail
(will show your gravatar icon)
Home page
2+5

Comment (Some html is allowed: a@href@title, b, blockquote@cite, em, i, li, ol, pre, strike, strong, sub, super, u, ul) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Live Comment Preview

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