Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

HEAD Verb in Minimal API C#

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);

>Solution :

Use MapMethods with corresponding verb (docs):

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading