title: ActionResult to String
body: <p>I know there are a lot of questions similar, but I didn't get the answer I wanted in any of them.
  I want to convert, as it is, an ActionResult to String, by doing something like that:</p>
  
  <pre><code>public ActionResult ProductDetail(int id)
  {
      // ... lots of operations that include usage of ViewBag and a Model ...
      return View(product);
  }
  </code></pre>
  
  <p>To show the product page to the client, but also I want to be able to send it by JSON, like:</p>
  
  <pre><code>public JsonResult ProductDetailJSON(int id)
  {
      // ... lots of operations that include usage of ViewBag and a Model ...
      return Json(new {
              html = this.ProductDetail(id).ToString(),
              name = ""
              // ... more properties
          }, JsonRequestBehavior.AllowGet);
  }
  </code></pre>
  
  <p>So I will <strong>invoke</strong> the Controller Action and get its response (a <code>ActionResult</code>, that is actually a <code>ViewResult</code>) and turn it into a <code>String</code>, but having it created using all the business code of the controller method (including, specially, <code>ViewBag</code> usage).</p>
  
  <p><strong>EDIT Clarification about why I want to to do it that way</strong></p>
  
  <p>I need to be able to present the product in the browser, so I create my Controller Action called <code>ProductDetail</code>, and works perfectly fine.</p>
  
  <p>After that comes the need to have an API that gives, for a given product a JSON object with some meta-data (like sold units, name, etc.) and the View. One option was to send a link to the view and that's all... but the view MUST be inlined as a client requirement.</p>
  
  <p>To avoid code repetition and to keep it clean, I'd like to invoke the method <code>ProductDetail</code>, and capture the HTML Raw response to put it inside the JSON response as an <code>String</code>.</p>
  
  <p>I've seen others rendering the View with methods like this one:</p>
  
  <pre><code>public string RenderRazorViewToString(string viewName, object model)
  {
      ViewData.Model = model;
      using (var sw = new StringWriter())
      {
          var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
          var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
          viewResult.View.Render(viewContext, sw);
          viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
          return sw.GetStringBuilder().ToString();
      }
  }
  </code></pre>
  
  <p>But that way <strong>does not execute the controller logic</strong>.</p>