emptygif
Language: English

All languages and frameworks

ASP.NET Core MVC controller C#

A C# snippet for ASP.NET Core MVC controller that answers requests for /pixel.gif with the empty GIF, decoded once at startup.

ASP.NET Core MVC controller C#

Install
dotnet new webapi --use-controllers
File
Program.cs
Run
dotnet run
using Microsoft.AspNetCore.Mvc;

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

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

[ApiController]
public class PixelController : ControllerBase
{
    private static readonly byte[] Gif = Convert.FromBase64String("R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==");

    [HttpGet("/pixel.gif")]
    [ResponseCache(NoStore = true)]
    public IActionResult Get() => File(Gif, "image/gif");
}

We ran this snippet and compared the bytes it returned.

Check it

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

Official site: ASP.NET Core MVC controller

More in C#