将HttpResponseBase转换为IHttpActionResult

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将HttpResponseBase转换为IHttpActionResult相关的知识,希望对你有一定的参考价值。

如何在IHttpActionResult操作,Web API控制器中实现以下功能?

我是否需要将HttpResponseBase修改为其他内容?

public void ZipFilesToResponse(HttpResponseBase response, IEnumerable<Asset> files, string zipFileName)
{
    using (var zipOutputStream = new ZipOutputStream(response.OutputStream))
    {
        zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
        response.BufferOutput = false;
        response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
        response.ContentType = "application/octet-stream";

        foreach (var file in files)
        {
            var entry = new ZipEntry(file.FilenameSlug())
            {
                DateTime = DateTime.Now,
                Size = file.Filesize
            };
            zipOutputStream.PutNextEntry(entry);
            storageService.ReadToStream(file, zipOutputStream);
            response.Flush();
            if (!response.IsClientConnected)
            {
               break;
            }
        }
        zipOutputStream.Finish();
        zipOutputStream.Close();
    }
    response.End();
}

在这里使用:

[HttpPost]
[Route("DownloadFiles/{company_id}")]
public IHttpActionResult DownloadFiles(Int64 company_id, TasksFilterModel searchModel)
{
     var files = .....;
     ZipFilesToResponse(_______,files,"download.zip");
     return ????
}
答案

您当前的ZipFilesToResponse方法更适用于常规MVC,而不是Web API。一种选择是简单地将此操作移动到MVC控制器而不是Web API控制器。

public class MyController : Controller
{
    [HttpPost]
    [Route("DownloadFiles/{company_id}")]
    public void DownloadFiles(Int64 company_id, TasksFilterModel searchModel)
    {
         var files = ...;
         ZipFilesToResponse(HttpContext.Response, files, "download.zip");
    }
}

您不需要返回任何内容,因为您的ZipFilesToResponse方法直接写入HttpContext.Response对象。

或者,如果你想坚持使用纯粹的Web API,我们可以修改ZipFilesToResponse方法来创建一个HttpResponseMessage,然后从你的动作中返回它。

[Route("DownloadFiles")]
[HttpGet]
public HttpResponseMessage DownloadFiles()
{
    var files = ...;
    return ZipContentResult(files, "download.zip");
}

protected HttpResponseMessage ZipContentResult(IEnumerable<Asset> files, string zipFileName)
{
    var pushStreamContent = new PushStreamContent((stream, content, context) =>
    {
        using (var zipOutputStream = new ZipOutputStream(stream))
        {
            zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression

            foreach (var file in files)
            {
                var entry = new ZipEntry(file.FilenameSlug())
                {
                    DateTime = DateTime.Now,
                    Size = file.Filesize
                };
                zipOutputStream.PutNextEntry(entry);
                storageService.ReadToStream(file, zipOutputStream);
                stream.Flush();
             }
             zipOutputStream.Finish();
             zipOutputStream.Close();
        }
        stream.Close();
    }, "application/zip");

    pushStreamContent.Headers.Add("Content-Disposition", "attachment; filename=" + zipFileName);

    return new HttpResponseMessage(HttpStatusCode.OK) { Content = pushStreamContent };
}

以上是关于将HttpResponseBase转换为IHttpActionResult的主要内容,如果未能解决你的问题,请参考以下文章

HttpResponse和JsonResponse

django之HTTPResponse和JsonResponse详解

04_HttpResponse对象及使用其设置cookie详解

Django HttpResponse与JsonResponse对象

强大的网络调试软件 iHTTP Tracker 猫头鹰

如何将curl的输出捕获到bash中的变量[重复]