- Copy + paste
Program.cs
andWebApp.csproj
into a directory dotnet restore
dotnet run
-
-
Save pimbrouwers/4b8131a0fc312af01cb3e205423f43b2 to your computer and use it in GitHub Desktop.
A minimal fully asynchronous C# ASP.NET Core 3.x application with routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk.Web"> | |
<PropertyGroup> | |
<TargetFramework>netcoreapp3.0</TargetFramework> | |
</PropertyGroup> | |
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Builder; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.Hosting; | |
namespace WebApp | |
{ | |
public class Program | |
{ | |
public static async Task<int> Main(string[] args) { | |
try { | |
var host = new WebHostBuilder() | |
.UseKestrel() | |
.UseUrls("http://localhost:5000/;https://localhost:5001") | |
.ConfigureServices(_configureServices) | |
.Configure(_configure) | |
.Build(); | |
await host.RunAsync(); | |
return 0; | |
} | |
catch { | |
return -1; | |
} | |
} | |
static Action<IServiceCollection> _configureServices = (services) => { | |
services.AddRouting(); | |
}; | |
static Action<IApplicationBuilder> _configure = app => { | |
app.UseRouting(); | |
app.UseEndpoints(r => { | |
r.MapGet("/hello/{name}", ctx => ctx.Response.WriteAsync("hello " + ctx.Request.RouteValues["name"])); | |
r.MapGet("/", ctx => ctx.Response.WriteAsync("Hello World!")); | |
}); | |
app.Run((ctx) => ctx.Response.WriteAsync("Page not found.")); | |
}; | |
} | |
} |
This is using the old routing system. Endpoint routing is the new one linked in the original gist. It's much faster 😄
I preferred the API of the IRouter
because it more explicitly exposed the parsed route data. But no brainer if the new Endpoint router is that much faster. Updated.
The route data is on the http request
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is using the old routing system. Endpoint routing is the new one linked in the original gist. It's much faster 😄