.net HttpClient.PostAsync响应.Content.Headers.ContentDisposition为null

pnwntuvh  于 2023-04-22  发布在  .NET
关注(0)|答案(1)|浏览(242)

我创建了一个Azure Function项目(.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;
    }

然后是Blazor WASM .NET 7.0项目,修改FetchData页面如下:

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()}");
        }
    }
}

但是,response.Content.Headers.ContentDisposition始终为null。
Fiddler显示的标题包括:
内容物处置:附件;文件名=“MyFile.txt”
什么是阻止HttpResponseMessage接收ContentDisposition?

6vl6ewon

6vl6ewon1#

解决方案是添加:

response.Headers.Add("Access-Control-Expose-Headers", "*");

相关问题