0

I download a file from an action which looks like this:

[HttpPost]
public override FileContentResult Foo() {
  var someDataAsBytes = ...
  return File(someDataAsBytes, "text/csv", "somefilename.csv");
}

The view has a form, which POSTs to this action.

I initiate the download in script, so that the page doesn't change:

$(myForm).on("submit", function() {
  window.location.href = $(this).attr("action");
});

The file is downloaded correctly.

But, my Elmah logs show that there was a "no matching action" error.

If I use [HttpHead], [HttpPost] or [HttpGet], I get the same result. If I remove the HTTP method, then I don't get an error.

Ideally I'd like to use POST, but that's not crucial. How do I restrict the action to one of these, but not get errors?

h bob
  • 3,610
  • 3
  • 35
  • 51

1 Answers1

0

The answer is that each time I click to download the file, two requests are sent, a POST and a HEAD.

If the action doesn't accept both, then exceptions are thrown. If there is no HTTP method specified, then both get through without error.

That's the answer to the question.

BUT, I don't know why there are two requests to begin with. I'll ask that in a separate question.

h bob
  • 3,610
  • 3
  • 35
  • 51