Home > Mobile >  HEAD Verb in Minimal API C#
HEAD Verb in Minimal API C#

Time:01-14

HEAD

The HTTP HEAD method requests the headers that would be returned if the HEAD request's URL was instead requested with the HTTP GET method.

For example, if a URL might produce a large download,a HEAD request could read its Content-Length header to check the file-size without actually downloading the file.

My question , how should I get that behavior in my minimal API endpoint.

when-ever I test my API Endpoint in vs-code through Thunder-Client, it return error when I select HEAD VERB

405 Method Not Allowed 

However the downloading is work through GET VERB.

I am very grateful of any example using HEAD VERB, or some settings which maybe I not know to configure.

code in this example is refer from following stack overflow question ASP.NET Minimal API How to Return/Download Files from URL

app.MapGet(@"download/{fileName}", async (string fileName) =>
{
    var mimeType = "application/zip";
    var path = @$"e:\{fileName}.zip";
    var bytes = await File.ReadAllBytesAsync(path);

    return Results.File(bytes, mimeType, $"{fileName}.zip");
})
          .Produces(StatusCodes.Status200OK)
          .Produces(StatusCodes.Status404NotFound)
          .WithName($"GET {EndpointCategory} by Name")
          .WithTags(Tag);

CodePudding user response:

Use MapMethods with corresponding verb (docs):

app.MapMethods(@"download/{fileName}", new[] { "HEAD" }, async (string fileName) => ...);
  • Related