I've created an Azure Function project (.NET Framework Isolated v4)
[Function("Function1")]
public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, "get", "post")] HttpRequestData req)
{
_logger.LogInformation("C# HTTP trigger function processed a request.");
var response = req.CreateResponse(HttpStatusCode.OK);
response.Headers.Add("Content-Type", "text/plain; charset=utf-8");
response.Headers.Add("Content-Disposition", $@"attachment; filename=""MyFile.txt""");
response.WriteString("test contents");
return response;
}
Then a Blazor WASM .NET 7.0 project, modifying the FetchData page as follows:
protected override async Task OnInitializedAsync()
{
var content = new StringContent("");
var response = await Http.PostAsync("http://localhost:7155/api/Function1", content);
if (response.IsSuccessStatusCode)
{
var contentDispositionHeader = response.Content.Headers.ContentDisposition;
if (contentDispositionHeader != null)
{
Console.WriteLine($"Content-Disposition: {contentDispositionHeader.ToString()}");
}
}
}
However, response.Content.Headers.ContentDisposition is always null.
Fiddler shows the headers including:
Content-Disposition: attachment; filename="MyFile.txt"
What is stopping HttpResponseMessage from receiving ContentDisposition?
Solution is to add:
response.Headers.Add("Access-Control-Expose-Headers", "*");