Download 如何从返回IHttpActionResult的Web Api2方法中的Byte[]数组下载文件?

Download 如何从返回IHttpActionResult的Web Api2方法中的Byte[]数组下载文件?,download,asp.net-web-api2,Download,Asp.net Web Api2,下面是我在ApicController类中得到的方法。 _fileContents字典填充在另一个WebApi2方法BuildContent(params)中。 当用户对BuildContent(params)方法进行ajax调用时,该方法将生成 字典的字符串结尾,它包含完整的HTML内容,包括一个表标记,并传回Guid,字典结尾。javascript依次执行以下操作: window.location = 'api/MyController/DownloadFile/' + guid; Ap

下面是我在ApicController类中得到的方法。 _fileContents字典填充在另一个WebApi2方法BuildContent(params)中。 当用户对BuildContent(params)方法进行ajax调用时,该方法将生成 字典的字符串结尾,它包含完整的HTML内容,包括一个表标记,并传回Guid,字典结尾。javascript依次执行以下操作:

window.location = 'api/MyController/DownloadFile/' + guid; 
ApiController静态:

private static Dictionary<Guid, String> _fileContents = new Dictionary<Guid, String>();
调用序列工作正常,但DownloadFile()方法引发以下异常:

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type             'application/xml; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'System.Net.Http.StreamContent' cannot be serialized. Consider marking it with the     DataContractAttribute attribute, and marking all of its members you want serialized with     the DataMemberAttribute attribute. If the type is a collection, consider marking it with     the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for         other supported types.
</ExceptionMessage>
<ExceptionType>
System.Runtime.Serialization.InvalidDataContractException
</ExceptionType>

发生了一个错误。
'ObjectContent'1'类型未能序列化内容类型'application/xml'的响应正文;字符集=utf-8'。
System.InvalidOperationException异常
发生了一个错误。
无法序列化类型“System.Net.Http.StreamContent”。考虑使用DATACONTractAttor属性对其进行标记,并标记要使用DATAMEMBAREATE属性序列化的所有成员。如果类型是集合,请考虑用CopyDATAcNoTractAttor标记它。有关其他受支持的类型,请参阅Microsoft.NET Framework文档。
System.Runtime.Serialization.InvalidDataContractException

有人能告诉我发生了什么,以及如何实现下载简单html文件的目标吗?

您解决了这个问题吗?只是遇到了同样的问题。
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type             'application/xml; charset=utf-8'.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace/>
<InnerException>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'System.Net.Http.StreamContent' cannot be serialized. Consider marking it with the     DataContractAttribute attribute, and marking all of its members you want serialized with     the DataMemberAttribute attribute. If the type is a collection, consider marking it with     the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for         other supported types.
</ExceptionMessage>
<ExceptionType>
System.Runtime.Serialization.InvalidDataContractException
</ExceptionType>