【问题标题】:Get the generated html file inside an action在动作中获取生成的 html 文件
【发布时间】:2014-03-28 16:30:35
【问题描述】:

我有一个提供简单视图的 MVC 控制器

public class MyController : Controller {
    [HttpGet]
    public ActionResult Index() {
        return View();
    }

    [HttpGet]
    public ActionResult ZipIndex() {
        // Get the file returned bu Index() and zip it 
        return File(/* zip stream */);
    }
}

从上面可以看出,我需要实现的是一个方法,它获取由 Index() 生成的 html,将其压缩并作为可以下载的文件返回。

我知道如何压缩,但我不知道如何获取 html。

【问题讨论】:

  • 查看stackoverflow.com/questions/4936809/… - 可能就是答案。
  • 如果用户需要页面的 HTML 内容,他们不能直接右键单击并另存为吗?
  • @RichardEverett 如果许多用户真的知道能够保存页面的源代码(并且有方便的方法 - 即在手机上),我会感到惊讶。请注意,结果可能不是 HTML(即 CSV) - 因此对于我来说,查看和下载同一视图的 zip 的简单方法看起来非常合理。
  • @AlexeiLevenkov。非常感谢。这样可行。我想知道是否有直接调用 Index() 方法的方法?您链接到的该方法需要模型作为参数。我在 Index() 中有很多代码来设置视图,并且不想在 ZipIndex() 中复制它。
  • @IlirDeda 我扩展了下面的回复,展示了一个使用视图渲染和 .NET 4.5 zip 技术的解决方案。

标签: c# asp.net-mvc asp.net-mvc-4


【解决方案1】:

查看此帖子http://approache.com/blog/render-any-aspnet-mvc-actionresult-to/。它提供了一种将任何 ActionResult 的输出呈现为字符串的简洁方法。

编辑

在上述文章中概述的技术的基础上,一个完整的解决方案可能如下所示

using System.IO;
using System.IO.Compression;
using System.Web;

public class MyController : Controller
{
    [HttpGet]
    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public FileContentResult ZipIndex()
    {
        // Render the View output: 
        var viewString = View("TheViewToRender").Capture(ControllerContext);
        // Create a zip file containing the resulting markup
        using (MemoryStream outputStream = new MemoryStream())
        {
            StreamReader sr = new StringReader(viewString);
            using (ZipArchive zip = new ZipArchive(outputStream, ZipArchiveMode.Create, false))
            {
                ZipArchiveEntry entry = zip.CreateEntry("MyView.html", CompressionLevel.Optimal);
                using (var entryStream = entry.Open())
                {
                    sr.BaseStream.CopyTo(entryStream);
                }
            }
            return File(outputStream.ToArray(), MediaTypeNames.Application.Zip, "Filename.zip");
        }
    }
}

public static class ActionResultExtensions {
    public static string Capture(this ActionResult result, ControllerContext controllerContext) {
        using (var it = new ResponseCapture(controllerContext.RequestContext.HttpContext.Response)) {
            result.ExecuteResult(controllerContext);
            return it.ToString();
        }
    }
}
public class ResponseCapture : IDisposable {
    private readonly HttpResponseBase response;
    private readonly TextWriter originalWriter;
    private StringWriter localWriter;
    public ResponseCapture(HttpResponseBase response) {
        this.response = response;
        originalWriter = response.Output;
        localWriter = new StringWriter();
        response.Output = localWriter;
    }
    public override string ToString() {
        localWriter.Flush();
        return localWriter.ToString();
    }
    public void Dispose() {
        if (localWriter != null) {
            localWriter.Dispose();
            localWriter = null;
            response.Output = originalWriter;
        }
    }
}

【讨论】:

  • 这似乎只在现在使用模型时才有效。当我使用模型时,它无法填充它。
猜你喜欢
  • 2012-09-13
  • 2014-04-18
  • 2013-08-26
  • 2010-12-06
  • 1970-01-01
  • 2010-09-09
  • 2016-09-06
  • 2010-12-31
  • 1970-01-01
相关资源
最近更新 更多