title: View.RenderAsync() causes Cannot access a closed Stream exception
body: <p>I'm developing net6.0 MVC app. I use partial view rendering to the string format in my app. I make Ajax request to the app and expect it to form a Json containing rendered partial view as a prop.
  There is a method I use to render partial to a string below</p>
  <pre><code>public static string RenderPartialViewToString(Controller controller, string partialPath, object model)
          {
              if (string.IsNullOrEmpty(partialPath))
              {
                  //Set Action name as partial name
                  partialPath = controller.GetActionNameFromRouteData();
              }
  
              controller.ViewData.Model = model;
  
              using (var sw = new StringWriter())
              {
                  // Find partial view file
                  var viewEngine = DependencyResolverHelper.GetService&lt;ICompositeViewEngine&gt;();
                  var viewResult = viewEngine.FindView(controller.ControllerContext, partialPath, false);
                  
                  var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw, new HtmlHelperOptions());
                  
                  viewResult.View.RenderAsync(viewContext).GetAwaiter().GetResult();
  
                  return sw.GetStringBuilder().ToString();
              }
          }
  </code></pre>
  <p>The problem is once I call <code>viewResult.View.RenderAsync(viewContext).GetAwaiter().GetResult()</code> it seems it captures and blocks Response.Body of current HttpContext so I can't form and return expected json anymore.
  There is custom json result class I use to form the response</p>
  <p><a href="https://i.stack.imgur.com/z3kMG.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z3kMG.png" alt="enter image description here" /></a></p>
  <p>The exception</p>
  <pre><code>System.ObjectDisposedException: Cannot access a closed Stream.
     at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count)
     at System.IO.MemoryStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)
  --- End of stack trace from previous location ---
     at System.IO.Pipelines.StreamPipeWriter.FlushAsyncInternal(Boolean writeToStream, ReadOnlyMemory`1 data, CancellationToken cancellationToken)
     at Web.Controllers.BaseCustomController`1.JsonNetResult.ExecuteResultAsync(ActionContext context) 
  </code></pre>
  <p>Could anyone assist?</p>