create a http server with a specific route and a specific status code in csharp

Here is an example of creating an HTTP server with a specific route and a specific status code in C# using the ASP.NET Core framework:

main.cs
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;

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

    public void Configure(IApplicationBuilder app)
    {
        app.UseRouting();

        var routeBuilder = new RouteBuilder(app);

        // set up route with specific path and HTTP method
        routeBuilder.MapGet("/my-route", context =>
        {
            context.Response.StatusCode = 200; // set specific status code
            return context.Response.WriteAsync("Hello, world!");
        });

        app.UseRouter(routeBuilder.Build());
    }
    
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup()
            .Build();

        host.Run();
    }
}
977 chars
40 lines

This code sets up an HTTP server with a specific route (/my-route) and a specific status code (200). When a GET request is made to the route, the server responds with the message "Hello, world!" and the specified status code.

gistlibby LogSnag