title: How to convert ASP.Net MVC 5 ExecuteResult to ASP.Net Core ExecuteResultAsync
body: <p>I'm migrating one of my Asp.Net MVC 5 project to Asp.Net Core. I have a custom ActionResult, which renders the view as partial view if the request was ajax request, and renders the view as normal view with layout if the request was a normal request. I have issues with converting the old ExecuteResult to new ExecuteResultAsync.</p>
  
  <p>Here it is my old code:</p>
  
  <pre><code>public override void ExecuteResult(ControllerContext context)
      {
          context.HttpContext.Response.Buffer = true;
          context.Controller.ViewData.Model = Model;
  
          using (var sw = new StringWriter())
          {
              var request = context.RequestContext.HttpContext.Request;
  
              if (string.IsNullOrEmpty(ViewName))
              {
                  ViewName = request.RequestContext.RouteData.GetRequiredString("action");
              }
  
              var viewResult = request.IsAjaxRequest()
                  ? ViewEngines.Engines.FindPartialView(context, ViewName)
                  : ViewEngines.Engines.FindView(context, ViewName, "_Layout");
  
              var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
              viewResult.View.Render(viewContext, sw);
              context.HttpContext.Response.Write(sw.GetStringBuilder().ToString());
          }
      }
  </code></pre>
  
  <p>And here it is my half converted code:</p>
  
  <pre><code>public override async Task ExecuteResultAsync(ActionContext context)
      {
          context.HttpContext.Response.Buffer = true; //broken
          context.HttpContext.Controller.ViewData.Model = Model; //broken
  
          using (var sw = new StringWriter())
          {
              var request = context.HttpContext.Request;
  
              if (string.IsNullOrEmpty(ViewName))
                  ViewName = context.ActionDescriptor.Name;
  
              var engine = context.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
  
              var viewResult = request.Headers["X-Requested-With"] == "XMLHttpRequest"
                  ? engine.FindPartialView(context, ViewName)
                  : engine.FindView(context, ViewName);
  
              var viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw, new HtmlHelperOptions()); //broken
              await viewResult.View.RenderAsync(viewContext);
              await context.HttpContext.Response.WriteAsync(sw.GetStringBuilder().ToString());
          }
      }
  </code></pre>
  
  <p>My main issue is the ControllerContext -> ActionContext migrating. Please help me to convert ControllerContext to ActionContext. Thank you very much!</p>