2017-01-06 57 views
0

我在舊的MVC項目中有以下代碼。我正在嘗試將其遷移到Asp.Net Core。任何人都有關於如何新的HttpResponse的想法,以便我可以遷移下面的代碼?如何在Asp.Net Core 1.0中創建新的HttpResponse實例和上下文

private string RenderActionResultToString(ActionResult result) 
     { 
      // Create memory writer. 
      var sb = new System.Text.StringBuilder(); 
      var memWriter = new System.IO.StringWriter(sb); 

     // Create fake http context to render the view. 
     var fakeResponse = new HttpResponse(memWriter); 
     var fakeContext = new HttpContext(System.Web.HttpContext.Current.Request, 
      fakeResponse); 
     var fakeControllerContext = new ControllerContext(
      new HttpContextWrapper(fakeContext), 
      this.ControllerContext.RouteData, 
      this.ControllerContext.Controller); 
     var oldContext = System.Web.HttpContext.Current; 
     System.Web.HttpContext.Current = fakeContext; 

     // Render the view. 
     result.ExecuteResult(fakeControllerContext); 

     // Restore old context. 
     System.Web.HttpContext.Current = oldContext; 

     // Flush memory and return output. 
     memWriter.Flush(); 
     return sb.ToString(); 
    }` 
+0

爲什麼你想要做的事,如「假」控制器和應對?只是好奇 ... –

回答

相關問題