Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Quick start

Fernando Escolar edited this page May 6, 2021 · 1 revision

Create a new web project:

mkdir MyApp
cd MyApp
dotnet new web

First of all, you have to add RoutingRecords package reference to your project:

dotnet add package RoutingRecords

Then you have two choices:

Single source code file

Delete all .cs files and add a new one with this content:

using RoutingRecords;

new ApiApp().Run();

record Hello() : Get("/", (req, res) =>
   res.SendAsync("Hello World!"));

And test your application:

dotnet run

Disclaimer: this solution is great for quick little APIs. But its use is not recommended in production applications.

Traditional way

In the StartUp file you have to call AddRouteRecords in the ConfigureServices and MapRouteRecords in the enpoints mapping section:

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using RoutingRecords;

namespace SampleApp
{
  public class Startup
  {
    public void ConfigureServices(IServiceCollection services)
    {
      services.AddRouteRecords(typeof(Startup).Assembly);
    }

    public void Configure(IApplicationBuilder app)
    {
      app.UseRouting();
      app.UseEndpoints(endpoints => endpoints.MapRouteRecords());
    }
  }
}

Now you can create a new file with your route record:

using RoutingRecords;

namespace SampleApp.Api
{
  public record Hello()
    : Get("/", (req, res) =>
        res.SendAsync("Hello World!"));
}

Finally test your application:

dotnet run