返回HttpResponseMessage时,.Net WebAPI没有响应

时间:2015-08-19 11:57:02

标签: c# asp.net .net asp.net-web-api asp.net-web-api2

我在回复HttpResponseMessage时遇到了与网络API相关的问题。这个对象我返回一个音频流,这个文件有大约60kb,并且在我第一次调用web api时成功,但是,在下一次web api没有响应并且浏览器只保持加载时。

下面是代码:

为什么这个web api在我下次打电话时不起作用/响应?

[HttpGet]
[Route("api/card/playAudio/{text}")]
public async Task<HttpResponseMessage> PlayAudio(string text)
{
    var audioStream = await _cardService.GetStream(text);
    return CreateAudioResponse(audioStream);
}

private HttpResponseMessage CreateAudioResponse(System.IO.Stream audioStream)
{
    var result = this.Request.CreateResponse(HttpStatusCode.OK);

    //HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
    result.Content = new StreamContent(audioStream);
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    result.Content.Headers.ContentDisposition.FileName = string.Format("{0}.mp3", new Guid().ToString());
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    result.Content.Headers.ContentLength = audioStream.Length;
    return result;
}

0 个答案:

没有答案