1
我希望能夠在Web API中返回對請求的複雜響應。例如,我想用一些複雜的對象返回流。 我試着用MultipartFormDataContent做到這一點:Web API中的Multipart內容響應
public static HttpResponseMessage GetMultipartResponse<T>(T responseData, Stream streamToReturn)
{
return new HttpResponseMessage
{
Content = new MultipartContent
{
new StreamContent(streamToReturn),
new ObjectContent<T>(responseData, new JsonMediaTypeFormatter())
}
};
}
我得在服務器端正常HttpResponseMessage
。我可以用我的調試器API的Web方法如何返回此響應見,但在客戶端上我得到了一個錯誤:
StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Pragma: no-cache
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNccnVzdGFtX3NhbGFraHV0ZGlub3ZcZG9jdW1lbnRzXHZpc3VhbCBzdHVkaW8gMjAxM1xQcm9qZWN0c1xGaWxlU2VydmljZUFQSVxGaWxlU2VydmljZUFQSVxhcGlcZ2V0?=
Cache-Control: no-cache
Date: Tue, 26 Jul 2016 08:30:22 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Length: 1444
Content-Type: application/json; charset=utf-8
Expires: -1
}
有客戶端或服務器端沒有其他異常,我無法得到一些與導通錯誤詳細信息
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
UPDATE: 嘗試讀取與在客戶端響應:
var result = await response.Content.ReadAsMultipartAsync();
並得到了這個例外:
Invalid 'HttpContent' instance provided. It does not have a content type header starting with 'multipart/'.
我在哪裏出錯了?
更新2: 我有在服務器端正常響應:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.MultipartContent, Headers:
{
Content-Type: multipart/multipart; boundary="3e6d3573-9031-41a7-b7f4-d1421bc1451d"
}