0
.csvファイルを返すだけです。Webアプリケーションの応答でByteArraycontent型をシリアル化できません
それはなぜHttpResponseMessageなくIHttpActionResultと
で動作しますか?
は
public async Task<HttpResponseMessage> ExportLeads()
{
byte[] bytes = new byte[2];
var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(bytes) };
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "test.csv" };
return result;
}
ではない
public async Task<IHttpActionResult> ExportLeads()
{
byte[] bytes = new byte[2];
var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(bytes) };
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = "test.csv" };
return Content(HttpStatusCode.OK, result);
}
エラー =>
The type "System.Net.Http.ByteArrayContent" can not be serialized.
Annotate it with the Attribut "DataContractAttribute"...
あなたは、特に 'NegotiatedContentResult'を返すようにしたいですか?そうでなければ、 'return Content(HttpStatusCode.OK、result)'とは対照的に 'return this.ResponseMessage(result)'を試してください。 – strickt01
これはうまくいった、ありがとう! – HelloWorld
偉大な、その旨の回答を掲載しました。 – strickt01