3

In my ASP.NET core Controller I've got the following HttpGet-function:

[HttpGet("[action]")]
[HttpHead("[action]")]
[ResponseCache(NoStore = true)]
public async Task<IActionResult> GetProofPdf(long studentid)
{
  var rawPdfData = await _studentLogic.StudentPdfAsync(User, studentid);
  if (Request.Method.Equals("HEAD"))
  {
    Response.ContentLength = rawPdfData.Length;
    return Json(data: "");
  }
  else
  {
    return File(rawPdfData, "application/pdf");
  }
}

This does work nicely. The returned file object can be saved from the browser. The only problem is embedding the PDF in IE. IE sends a HEAD request first. The HEAD request Fails, so IE does not even try to get the PDF. Other browsers do not send HEAD or use GET when HEAD fails, but not IE.

Since I want so Support IE I want to create a HEAD Action. Just adding [HttpHead("[action]")] to the function will not work, probably because for HEAD the content must be empty ("The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.").

So how do I create a HttpHead-Verb-Function in ASP.NET Core? How do I return empty Content but the correct content-length?

4

1 回答 1

2

这些方面的东西应该适合你。

    [HttpGet]
    [HttpHead]
    [ResponseCache(NoStore = true)]
    public async Task<IActionResult> GetProofPdf(long studentid)
    {
        if (Request.Method.Equals("HEAD"))
        {
            //Calculate the content lenght for the doc here?
            Response.ContentLength = $"You made a {Request.Method} request".Length;
            return Json(data: "");
        }
        else
        {
            //GET Request, so send the file here.
            return Json(data: $"You made a {Request.Method} request");
        }
    }
于 2017-12-12T16:01:43.010 回答