emptygif
言語: 日本語

すべての言語・フレームワーク

FastEndpoints C#

/pixel.gif へのリクエストに空の GIF を返す、FastEndpoints 用の C# スニペット。GIF は起動時に一度だけデコードします。

FastEndpoints C#

インストール
dotnet add package FastEndpoints
ファイル
Program.cs
実行
dotnet run
using FastEndpoints;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddFastEndpoints();

var app = builder.Build();
app.UseFastEndpoints();
app.Run("http://0.0.0.0:8080");

sealed class PixelEndpoint : EndpointWithoutRequest
{
    static readonly byte[] Gif = Convert.FromBase64String("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");

    public override void Configure()
    {
        Get("/pixel.gif");
        AllowAnonymous();
    }

    public override async Task HandleAsync(CancellationToken ct)
    {
        HttpContext.Response.Headers.CacheControl = "no-store";
        await Send.BytesAsync(Gif, contentType: "image/gif", cancellation: ct);
    }
}

このスニペットは実行し、返されたバイト列を照合しました。

確認

curl -sI http://localhost:8080/pixel.gif

公式サイト: FastEndpoints

C# の他の例