w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
out of memory exception on StringWriter using MVC, can memory be increased?

You're writing the view output to a string, then writing it to a response. What you may want to do is skip the middle-man, and write the view directly to the response. Since the Render method accepts any TextWriter, you can pass in Response.OutputStream to your StreamWriter instead of just writing to a plain string.

So something like:

System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Headers["content-disposition"] = 
    "attachment;filename=somefile.txt"; // or whatever
System.Web.HttpContext.Current.Response.Headers["content-type"] =
    "text/plain"; // or whatever
using (var writer = new
StreamWriter(System.Web.HttpContext.Current.Response.OutputStream))
{
    var view = ViewEngines.Engines.FindView(_context, _viewName, null).View
        as RazorView;
    var viewDataDictionary = new ViewDataDictionary<Foo>(_model);
    var viewCtx = new ViewContext(_context, 
        view, viewDataDictionary, new TempDataDictionary(), writer);
    viewCtx.View.Render(viewCtx, writer);
}
return null;




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.