title: One MVC3 Controller method two different ActionResult types?
body: <p>Currently I have a Controller, AssetController, that has methods for navigating a tree of assets.</p>
  
  <p>I use this controller to navigate a "gallery" of assets and the controller methods currently return <code>ViewResult</code>. </p>
  
  <p>I am starting to build another way of viewing this same data which will make use of ajax and will not want <code>ViewResult</code> returned but rather <code>JsonResult</code>. </p>
  
  <p>I would like to re-use as much of my controller logic as possible. I can think of two options here:</p>
  
  <ol>
  <li><p>Change all my controller methods to return only <code>JsonResult</code> then change my current View to use the json to display the full page.</p>
  
  <p><strong>Drawbacks</strong>: This pushes more page loading / redirecting logic onto the client. It also make deep linking a problem I think. With full views the URL's of pages are very "link friendly" in that a user can copy and paste a link and pass it to someone and it will go where they expect. Returning JsonResults mean the URL doesn't change so deep linking becomes more challenging </p></li>
  <li><p>Refactor my current controller so that "shared" code is moved into private methods in the controller. Create new actions that return <code>JsonResult</code> and keep existing methods that return ViewResult.</p>
  
  <p>This is currently my choice as it seems to keep more code in the controller which makes unit testing it easier. But it also seems to be a little too coupled to the UI, what happens when I want to return the data in different formats, do I just keep adding new controller actions with different <code>ActionResult</code> types? Is that so bad?</p></li>
  </ol>
  
  <p><strong>Is there a smarter way to handle situation or is one of my approaches more favorable than the other?</strong></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Here is some code examples to clarify:</p>
  
  <p>Currently I have this:</p>
  
  <pre><code>public ActionResult ViewObject(PathModel inPath)
  {
      using (engine)
      {
        var metaData = engine.GetFileMetaData(inPath.Path);
    return View(metaData); //This is returning a ViewResult type
      }
  }
  </code></pre>
  
  <p>I am thinking about doing something like this</p>
  
  <pre><code>private FileMetaDataModel ViewObjectInternal(PathModel inPath)
  {               
    using (engine)
    {
        var metaData = engine.GetFileMetaData(inPath.Path);
        return metaData;
    }
  }
  
  public ActionResult ViewObjectAsJSON(PathModel inPath)
  {
     var jsonResult = new JsonResult();
     jsonResult.Data = ViewObjectInternal(inPath).ToJson();
  
     return jsonResult // this is returning my data as JSON;
  }
  
  public ActionResult ViewObjectAsView(PathModel inPath)
  {
    return View(engine.GetFileMetaData(inPath.Path)); //This is returning a ViewResult type
  }
  </code></pre>