title: Overriding / Extending an MVC Controller / Area
body: <p>I'm currently working on an MVC project and I'm trying to figure out how I might go about extending the routes of an existing Controller within an Area, specifically from another project.</p>
  
  <p>For instance, I have a Controller with an area that looks like the following :</p>
  
  <pre><code>namespace MyProject.Areas.Foo.Controllers
  {
      [Authorize]
      public class FooController : ApplicationController
      {
            //code
      }
  }
  </code></pre>
  
  <p>And what I would like to do, is be able to define another Controller, within a separate project that could extend this like so :</p>
  
  <pre><code>namespace MyOtherProject.Areas.Foo.Custom.Controllers
  {
      public class FooController : ApplicationController
      {
            public string Bar()
            {
                 return "Bar";
            }
      }
  }
  </code></pre>
  
  <p>Basically, I would like the controllers to almost function as if I was using the <code>partial</code> keyword (so that I could call any of the actions in the original or the new one).</p>
  
  <p><strong>The Main Problem</strong></p>
  
  <p>What I am really trying to accomplish is that I have a main project with several areas and another area of my solution with various client folders. I want to be able to essentially extend the base controllers for my main project and add client-specific actions within these client folders so that they can be used in the main project. I'm already doing this with certain MVC Views, but I was hoping I could accomplish it with controllers as well.</p>
  
  <p><strong>What I've tried</strong></p>
  
  <ul>
  <li>I tried using the <code>partial</code> keyword on both declarations of the class, but since they are in different projects / assemblies, I don't think that works.</li>
  <li>I defined a build event that would move the custom DLL into the <code>bin</code> directory of the main MVC project, but that didn't seem to work as expected.</li>
  <li>I've tried various approaches for inheritance, hoping the new class would get picked up, but those didn't work (received the duplicate controller declaration error).</li>
  <li>I've read about trying to use a custom <code>ControllerFactory</code> but I wasn't sure how to implement it.</li>
  <li>I've tried defining custom namespace routing parameters in the AreaRegistration section to pick up the new controller like the following example.</li>
  </ul>
  
  <p><strong>Routing Example (AreaRegistration)</strong></p>
  
  <pre><code>context.MapRoute(
      AreaName,
      String.Format("{0}/{{action}}/{{id}}", AreaName),
      new { controller = AreaName, action = "Index", id = UrlParameter.Optional },
      new[] { 
          String.Format("MyProject.Areas.{0}.Controllers", AreaName),
          String.Format("MyOtherProject.Areas.{0}.Custom.Controllers", AreaName)
      }
  );
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I attempted an approach <a href="https://gist.github.com/anonymous/2c83cb28ae8d452f63812fc44eee6dae">seen here</a> as per some of the comments discussion that involved simply handling this via inheritance :</p>
  
  <pre><code>// Main Project
  namespace MyProject.Areas.Foo.Controllers
  {
      [Authorize]
      public class FooController : ApplicationController
      {
            public ActionResult Index()
            {
                return View();
            }
      }
  }
  
  // This is in another project / namespace / assembly
  namespace MyOtherProject.Foo.Controllers
  {
      public class CustomFooController : MyProject.Areas.Foo.Controllers.FooController
      {
          [Route("Foo/Bar")]
          public string Bar()
          {
              return "Bar";
          }
      }
  }
  </code></pre>
  
  <p>So my current steps are as follows :</p>
  
  <ul>
  <li>Inherited from the base <code>FooController</code> in the main project within another project / solution.</li>
  <li>Set up attribute routing to access the custom controller to avoid conflicting routes from the main project.</li>
  <li>Created a Build Event that moves the custom DLL into the main project when built (so it will be accessible) from the new custom project.</li>
  </ul>
  
  <p>This didn't seem to make any difference. I tried going to the <code>Foo/Bar</code> url but it just threw a 404 as if it didn't see it at all. The <code>CustomFooController.cs</code> file is in it's own separate project and is just a class file and not an MVC project. Is this correct? Do I need to set the routing rules in the main project?</p>