I have the following ASP.NET 4 code I found that I tried converting to ASP.NET 5 RC1 / ASP.NET Core
public async Task MyActionName(MyViewModel viewModel)
{
//some stuff
int accountId=1;
string _contentDisposition =
string.Format("attachment; filename=portfolio-inquiry-{0}.xlsx",
accountId.ToString());
Response.ContentType =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", _contentDisposition);
Response.BinaryWrite(_xlPackage.GetAsByteArray());
}
There is a GitHub answer posted that says for ASP.NET 5 I need to use
await context.Response.Body.WriteAsync(buffer, 0, buffer.Length);
So I change it for ASP.NET 5 to (not sure if it is correct)
public async Task MyActionName(MyViewModel viewModel)
{
//some stuff
int accountId=1;
string _contentDisposition =
string.Format("attachment; filename=portfolio-inquiry-{0}.xlsx", accountId.ToString());
var buffer = _xlPackage.GetAsByteArray();
await HttpContext.Response.Body.WriteAsync(buffer, 0, buffer.Length);
}
My view looks like this
<form action="@(Url.Action("MyActionName", "Controller"))" data-ajax="true" data-ajax-method="POST" method="post">
//form inputs
<button type="submit" class="btn btn-primary">Get file</button>
</form>
And the code run through without an error. However since the HttpContext.Response.Body.WriteAsync
or the old return is void I'm not sure how to return it back to my MVC view as the above code runs through correctly but since it doesn't return anything to the view, the view doesn't get the file.
I have run Fiddler to see if anything gets sent to the browser from the controller but nothing. In visual Studio I can step through all the code.
I think a possible issue is because ASP.NET 5 doesn't have Response.AddHeader
method so I excluded that line to see if it still works.
I then tried to convert it to a file and send that back to the view by chaning
return File(buffer, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
and changed my method return type to async Task<FileResult>
.
However the result is still the same. Code steps through the controller but doesn't return anything to the view. Fiddler picks up nothing. Debugging picks up no error. JavaScript console throws no error.