Web API将HttpResponseMessage的描述返回到浏览器

时间:2018-10-19 20:58:50

标签: c# asp.net-core-webapi

该代码应该导致文件下载...

public HttpResponseMessage Export()
{
    var byteArray = Encoding.UTF8.GetBytes("Hello World!");

    var stream = new MemoryStream(byteArray);

    var result = new HttpResponseMessage(HttpStatusCode.OK)
    {
        Content = new StreamContent(stream),
    };

    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "myFile.csv"
    };

    result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");

    return result;
}

相反,我收到此消息...

  

{“版本”:{“主要”:1,“次要”:1,“构建”:-1,“修订”:-1,“主要修订”:-1,“次要修订”:-1}, “ content”:{“ headers”:[]},“ statusCode”:200,“ reasonPhrase”:“ OK”,“ headers”:[],“ requestMessage”:null,“ isSuccessStatusCode”:true}

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

r