4

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

 3 years ago
source link: https://www.hanselman.com/blog/the-open-source-carter-community-project-adds-opinionated-elegance-to-aspnet-core-routing
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
The open source Carter Community Project adds opinionated elegance to ASP.NET Core routing
Sponsored By
image
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 Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK