emptygif
ภาษา: ไทย

ภาษาและเฟรมเวิร์กทั้งหมด

FastEndpoints C#

ตัวอย่างโค้ด C# สำหรับ FastEndpoints ที่ตอบคำขอไปยัง /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#