我需要構建一個以位元組陣列形式回傳 JSON 物件和附加檔案的 API。所以為了做到這一點,我想到了在 C# 中使用 MultipartFormDataContent 類。我的 API 寫了這樣的東西。
public async Task<IHttpActionResult> MethodName(Params){
......statements......
var responseContent = new MultipartFormDataContent();
......statements......
responseContent.Add(new StringContent(jsonString), 'JSON Object');
......statements......
responseContent.Add(byteArrayContent);
return responseContent;
}
這在構建專案時出現錯誤,無法將 responseContent 轉換為 IHttpActionResult。所以我嘗試了像這樣的顯式轉換。
return (IHttpActionResult)responseContent;
但這會引發關于無法轉換為 IHttpActionResult 的運行時錯誤。那么我將如何回傳 responseContent。任何幫助表示贊賞。
uj5u.com熱心網友回復:
回傳ResponseMessageResult,它支持IHttpActionResult:
return new ResponseMessageResult(new HttpResponseMessage() { Content = responseContent })
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/447549.html
