简体   繁体   中英

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.

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

Use MapMethods with corresponding verb ( docs ):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM