emptygif
語言: 繁體中文

所有語言和框架

FastEndpoints C#

適用於 FastEndpoints 的 C# 程式碼片段,以空 GIF 回應對 /pixel.gif 的請求,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# 範例