All Projects → CarterCommunity → Carter

CarterCommunity / Carter

Licence: mit
Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Carter

Ocelot
.NET core API Gateway
Stars: ✭ 6,675 (+662.86%)
Mutual labels:  middleware, asp-net-core, asp-net, dotnet-core
Hotchocolate
Welcome to the home of the Hot Chocolate GraphQL server for .NET, the Strawberry Shake GraphQL client for .NET and Banana Cake Pop the awesome Monaco based GraphQL IDE.
Stars: ✭ 3,009 (+243.89%)
Mutual labels:  asp-net-core, asp-net, dotnet-core
Framework
.NET Core Extensions and Helper NuGet packages.
Stars: ✭ 399 (-54.4%)
Mutual labels:  asp-net-core, asp-net, dotnet-core
Awesome Microservices Netcore
💎 A collection of awesome training series, articles, videos, books, courses, sample projects, and tools for Microservices in .NET Core
Stars: ✭ 865 (-1.14%)
Mutual labels:  asp-net-core, asp-net, dotnet-core
Templates
.NET project templates with batteries included, providing the minimum amount of code required to get you going faster.
Stars: ✭ 2,864 (+227.31%)
Mutual labels:  asp-net-core, asp-net, dotnet-core
Cofoundry
Cofoundry is an extensible and flexible .NET Core CMS & application framework focusing on code first development
Stars: ✭ 621 (-29.03%)
Mutual labels:  asp-net-core, asp-net, dotnet-core
Nopcommerce
The most popular open-source eCommerce shopping cart solution based on ASP.NET Core
Stars: ✭ 6,827 (+680.23%)
Mutual labels:  asp-net-core, asp-net, dotnet-core
Websocket Manager
Real-Time library for ASP .NET Core
Stars: ✭ 400 (-54.29%)
Mutual labels:  middleware, dotnet-core
Netcorebbs
ASP.NET Core Light forum NETCoreBBS
Stars: ✭ 483 (-44.8%)
Mutual labels:  asp-net-core, dotnet-core
Pieshopcore
A simple pie shopping management system using ASP.NET CORE MVC application
Stars: ✭ 25 (-97.14%)
Mutual labels:  asp-net-core, dotnet-core
Orchardcore
Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
Stars: ✭ 5,591 (+538.97%)
Mutual labels:  asp-net, dotnet-core
Comcms core
COMCMS_Core 版本
Stars: ✭ 377 (-56.91%)
Mutual labels:  asp-net-core, dotnet-core
Umbraco Cms
The simple, flexible and friendly ASP.NET CMS used by more than 730.000 websites
Stars: ✭ 3,484 (+298.17%)
Mutual labels:  dotnet-core, asp-net-core
Aspnet Core React Template
ASP.NET Core 3.1 / React SPA Template App
Stars: ✭ 539 (-38.4%)
Mutual labels:  asp-net-core, dotnet-core
Awesome Blazor
Resources for Blazor, a .NET web framework using C#/Razor and HTML that runs in the browser with WebAssembly.
Stars: ✭ 6,063 (+592.91%)
Mutual labels:  asp-net-core, dotnet-core
Exceptionless.net
Exceptionless clients for the .NET platform
Stars: ✭ 362 (-58.63%)
Mutual labels:  asp-net-core, asp-net
Starwars
GraphQL 'Star Wars' example using GraphQL for .NET, ASP.NET Core, Entity Framework Core
Stars: ✭ 559 (-36.11%)
Mutual labels:  asp-net-core, dotnet-core
Electron.net
Build cross platform desktop apps with ASP.NET Core (Razor Pages, MVC, Blazor).
Stars: ✭ 6,074 (+594.17%)
Mutual labels:  asp-net-core, dotnet-core
Mix.core
🚀 Mixcore CMS is an open source CMS that support both headless and decoupled to easily build any kinds of app/web app/customisable APIs built on top of ASP.NET Core / Dotnet Core. It is a completely open source ASP.NET Core (Dotnet Core) CMS solution. https://mixcore.org
Stars: ✭ 304 (-65.26%)
Mutual labels:  asp-net-core, dotnet-core
Simplcommerce
A simple, cross platform, modularized ecommerce system built on .NET Core
Stars: ✭ 3,474 (+297.03%)
Mutual labels:  asp-net-core, dotnet-core

Carter

NuGet Version feedz.io Build Status

Carter is a framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing the code to be more explicit and most importantly more enjoyable.

Carter simply builds on top of ASP.NET Core allowing you to have more elegant routing rather than attribute routing, convention routing, or ASP.NET Controllers.

For a better understanding, take a good look at the samples inside this repo. The samples demonstrate usages of elegant extensions around common ASP.NET Core types as shown below.

Other extensions include:

  • Bind/BindAndValidate<T> - FluentValidation extensions to validate incoming HTTP requests.

  • BindFile/BindFiles/BindFileAndSave/BindFilesAndSave - Allows you to easily get access to a file/files that has been uploaded. Alternatively you can call BindFilesAndSave and this will save it to a path you specify.

  • Before/After hooks to the routes defined in a Carter module.

  • Routes to use in common ASP.NET Core middleware e.g., app.UseExceptionHandler("/errorhandler");.

  • IStatusCodeHandlers are also an option as the ASP.NET Core UseStatusCodePages middleware is not elegant enough IMO. IStatusCodeHandlers allow you to define what happens when one of your routes returns a specific status code. An example usage is shown in the sample.

  • IResponseNegotiators allow you to define how the response should look on a certain Accept header. Handling JSON is built in the default response but implementing an interface allows the user to choose how they want to represent resources.

  • All interface implementations for Carter components are registered into ASP.NET Core DI automatically. Implement the interface and off you go.

  • Supports two different routing APIs.

    (i)

    this.Get("/actors/{id:int}", async (req, res) =>
    {
        var person = actorProvider.Get(req.RouteValues.As<int>("id"));
        await res.Negotiate(person);
    });
    

    (ii)

    this.Get("/actors/{id:int}", async (ctx) =>
    {
        var person = actorProvider.Get(ctx.Request.RouteValues.As<int>("id"));
        await ctx.Response.Negotiate(person);
    });
    

Endpoint Routing

Carter supports endpoint routing and all the extensions IEndpointConventionBuilder offers. For example you can define a route with authorization required like so:

this.Get("/", (req, res) => res.WriteAsync("There's no place like 127.0.0.1")).RequireAuthorization();

OpenApi

Carter supports OpenApi out of the box. Simply call /openapi from your API and you will get a OpenApi JSON response.

To configure your routes for OpenApi simply supply the meta data class on your routes. For example:

this.Get<GetActors>("/actors", async (req, res) =>
{
    var people = actorProvider.Get();
    await res.AsJson(people);
});

The meta data class is the way to document your routes and looks something like this:

public class GetActors : RouteMetaData
{
    public override string Description { get; } = "Returns a list of actors";

    public override RouteMetaDataResponse[] Responses { get; } =
    {
        new RouteMetaDataResponse
        {
            Code = 200,
            Description = $"A list of {nameof(Actor)}s",
            Response = typeof(IEnumerable<Actor>)
        }
    };

    public override string Tag { get; } = "Actors";
}

Where does the name "Carter" come from?

I have been a huge fan of, and core contributor to Nancy, the best .NET web framework, for many years, and the name "Nancy" came about due to it being inspired from Sinatra the Ruby web framework. Frank Sinatra had a daughter called Nancy and so that's where it came from.

I was also trying to think of a derivative name, and I had recently listened to the song Empire State of Mind where Jay-Z declares he is the new Sinatra. His real name is Shaun Carter so I took Carter and here we are!

CI Builds

If you'd like to try the latest builds from the master branch add https://f.feedz.io/carter/carter/nuget/index.json to your NuGet.config and pick up the latest and greatest version of Carter.

Getting Started

You can get started using either the template or by adding the package manually to a new or existing application.

Template

https://www.nuget.org/packages/CarterTemplate/

  1. Install the template - dotnet new -i CarterTemplate

  2. Create a new application using template - dotnet new Carter -n MyCarterApp

  3. Run the application - dotnet run

Package

https://www.nuget.org/packages/Carter

  1. Create a new empty ASP.NET Core application - dotnet new web -n MyCarterApp

  2. Change into the new project location - cd ./MyCarterApp

  3. Add Carter package - dotnet add package carter

  4. Modify your Startup.cs to use Carter

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCarter();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();
        app.UseEndpoints(builder => builder.MapCarter());
    }
}
  1. Create a new Module
    public class HomeModule : CarterModule
    {
        public HomeModule()
        {
            Get("/", async (req, res) => await res.WriteAsync("Hello from Carter!"));
        }
    }
  1. Run the application - dotnet run

Sample

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IActorProvider, ActorProvider>();
        services.AddCarter();
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();
        app.UseEndpoints(builder => builder.MapCarter());
    }
}

public class ActorsModule : CarterModule
{
    public ActorsModule(IActorProvider actorProvider)
    {
        this.Get("/actors", async (req, res) =>
        {
            var people = actorProvider.Get();
            await res.AsJson(people);
        });

        this.Get("/actors/{id:int}", async (req, res) =>
        {
            var person = actorProvider.Get(req.RouteValues.As<int>("id"));
            await res.Negotiate(person);
        });

        this.Put("/actors/{id:int}", async (req, res) =>
        {
            var result = req.BindAndValidate<Actor>();

            if (!result.ValidationResult.IsValid)
            {
                res.StatusCode = 422;
                await res.Negotiate(result.ValidationResult.GetFormattedErrors());
                return;
            }

            // Update the user in your database

            res.StatusCode = 204;
        });

        this.Post("/actors", async (req, res) =>
        {
            var result = req.BindAndValidate<Actor>();

            if (!result.ValidationResult.IsValid)
            {
                res.StatusCode = 422;
                await res.Negotiate(result.ValidationResult.GetFormattedErrors());
                return;
            }

            // Save the user in your database

            res.StatusCode = 201;
            await res.Negotiate(result.Data);
        });

    }
}

More samples

Configuration

Custom Model Binders

By default, Carter uses the System.Text.Json library for model binding, though you can use your own model binder by implementing the provided IModelBinder interface. You would then wire up your custom implmentation (say, CustomModelBinder) by adding the following line to the initial Carter configuration, in this case as part of Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCarter(configurator: c =>
        {
            c.WithModelBinder<CustomModelBinder>();
        });
    }

Note that Carter already ships with an alternate model binder implementation that uses Newtonsoft.Json, so you can switch to the Newtonsoft implementation with the following line (plus the requisite using statement):

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCarter(configurator: c =>
        {
            c.WithModelBinder<NewtonsoftJsonModelBinder>();
        });
    }

Custom response negotiators

Similar to model binding, on the outbound side of things, Carter will use a response negotiator based on System.Text.Json, though it provides for custom implementations via the IResponseNegotiator interface. To use your own implementation of IResponseNegotiator (say, CustomResponseNegotiator), add the following line to the initial Carter configuration, in this case as part of Startup.cs:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCarter(configurator: c =>
        {
            c.WithResponseNegotiator<CustomResponseNegotiator>();
        });
    }

Here again, Carter already ships with a response negotiator using Newtonsoft.Json, so you can wire up the Newtonsoft implementation with the following line:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCarter(configurator: c =>
        {
            c.WithResponseNegotiator<NewtonsoftJsonResponseNegotiator>();
        });
    }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].