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.

6年前写了有关NancyFX的博客,从那以后,我就建立并改进了许多ASP.NET开源框架。 -.NET上的Web开发已变得流行。

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.

Jonathan Channon和Carter社区(JC也是Nancy的核心贡献者)已经在ASP.NET Core的基础上制作了一层薄薄的扩展方法和约定,以使URL路由“更加优雅”。 Carter添加并正式化了一个更自以为是的框架,还添加了对惊人的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.

关于ASP.NET Core的最好的事情之一就是它的可扩展性模型,而Carter充分利用了这一点。 卡特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:

您只需通过“ dotnet add package carter ”并将其添加到您的Startup.cs中,就可以将Carter添加到您现有的ASP.NET Core应用程序中:

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:

在这一点上,您可以快速创建一个“微服务”(在这种情况下,只需处理一个HTTP GET),几乎不需要编写任何代码,而且阅读起来非常清晰:

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.

或者,您可以将Carter添加为模板,以便以后可以“ dotnet new carter 。 首先添加带有“ dotnet new -i CarterTemplate ”的卡特模板,现在您可以随时创建一个新的样板启动器应用程序。

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社区GitHub上有很多很棒的示例代码。 前往https://github.com/CarterCommunity/Carter/tree/master/samples ,给他们更多的星星!

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:

Carter还可以与现有的ASP.NET应用程序完美集成,因为它再次是对ASP.NET的扩展和改进。 现在,您可以将Carter添加到以MVC模式使用Controllers的ASP.NET Core应用中,如下所示:

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:

然后,通过返回诸如JSON这样的内容列表轻松处理GET:

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!

再次,在GitHub上查看Carter ,然后在Twitter上关注https://twitter.com/CarterLibs

翻译自: https://www.hanselman.com/blog/the-open-source-carter-community-project-adds-opinionated-elegance-to-aspnet-core-routing

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐