如何从 MVC 视图返回流?

发布于 2024-12-23 09:30:00 字数 632 浏览 0 评论 0原文

我相信我已经相当接近了,但我的流要么为空,要么已被处置。这是示例代码。

var ms = new MemoryStream();
using (var sw = new StreamWriter(ms))
{
   using (var tw = new HtmlTextWriter(sw))
   {
    ViewEngineResult viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, null);
    ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
    viewResult.View.Render(viewContext, tw);
    ms.Position = 0;
    return ms;
    }
}

调用代码会导致 objectDisposeException,因为流已被释放。如果我将 return 移到 using 之外,则结果为 null。我在这里做错了什么?我有什么想法可以让它正常工作吗?

I'm fairly close I believe but my stream is either null or its been disposed. Here is the sample code.

var ms = new MemoryStream();
using (var sw = new StreamWriter(ms))
{
   using (var tw = new HtmlTextWriter(sw))
   {
    ViewEngineResult viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, null);
    ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
    viewResult.View.Render(viewContext, tw);
    ms.Position = 0;
    return ms;
    }
}

the calling code results in an objectDisposedException because the stream is disposed. If I move the return outside of the using, the result is null. What am I doing wrong here? Any ideas how I can this to work correctly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忱杏 2024-12-30 09:30:00

StreamWriter 上的 using 语句也将关闭底层流。
删除 StreamWriter 上的 using 语句以保持流处于活动状态。
另一种选择是返回字节数组而不是内存流。

return ms.ToArray();

The using statement on the StreamWriter will also close the underlying stream.
Remove the using statement on the StreamWriter to keep the stream alive.
Another option would be to return a byte array rather than a memory stream.

return ms.ToArray();
雪落纷纷 2024-12-30 09:30:00

要在 using 块中使用某个类,该类必须实现 IDisposable。当块结束时,它会调用 IDisposable.Dispose() 来处置您的 Stream。这是一个很好的做法,但如果您想要流,请删除 using 或将 return 放入其中。我会选择第二个选择。

To use a class in a using block, that class must implement IDisposable. When the block ends, it invokes IDisposable.Dispose() which disposes your Stream. This is a good practice, but if you want the stream, either remove the using or put the return inside it. I would go with the second option.

坦然微笑 2024-12-30 09:30:00

你有 2 个 using,他说将 return 放在外部 using() 中,

如下所示

var ms = new MemoryStream();
using (var sw = new StreamWriter(ms))
{
   using (var tw = new HtmlTextWriter(sw))
   {
    ViewEngineResult viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, null);
    ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
    viewResult.View.Render(viewContext, tw);
    ms.Position = 0;
    }
  return ms;
}

You have 2 usings he is saying put the return inside of the outter using()

like this

var ms = new MemoryStream();
using (var sw = new StreamWriter(ms))
{
   using (var tw = new HtmlTextWriter(sw))
   {
    ViewEngineResult viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, null);
    ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
    viewResult.View.Render(viewContext, tw);
    ms.Position = 0;
    }
  return ms;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文