Extending MVC Controllers From External Assemblies And Projects

Recently, a question was asked on Stack Overflow regarding how to go about extending an existing ASP.NET MVC Controller that was present within another assembly or project (i.e. external to the "main" MVC application), so I thought I would take a bit of time to cover how one might handle this scenario.

The Basic Idea

Let's say you have two projects,

  • ProjectA Your main MVC application.
  • ProjectB Just a simple class library.

What you want to do is extend an existing controller that is present in ProjectA with additional controller actions or functionality. These additional actions will not be present in your main application, but instead will come from a different project / assembly. This could be useful for implementing client-specific functionality (i.e. a client wants certain behavior that may not be relevant in the main application).

So for instance, your controller definition in ProjectA might look something like this, 

  1. namespace ProjectA.Controllers    
  2. {  
  3.     // This is the primary controller that we want to extend  
  4.     public class FooController : ApplicationController  
  5.     {  
  6.           public ActionResult Index()  
  7.           {  
  8.               return Content("Foo");  
  9.           }  
  10.     }  
  11. }   

And you might have a similar class in ProjectB that resembles this, 

  1. namespace ProjectB.Controllers    
  2. {  
  3.     // You want to essentially add the Bar action to the existing controller  
  4.     public class CustomFooController : FooController  
  5.     {  
  6.         public ActionResult Bar()  
  7.         {  
  8.             return Content("Bar");  
  9.         }  
  10.     }  
  11. }   

You want to allow all of the different clients to access Foo, but perhaps only a certain client to be exposed to Foo/Bar.

Breaking Down The Steps

This process requires a few different steps that will need to be done to get everything working as expected, which I'll review below,

  1. Inheritance
    The custom controller will inherit from the controller in our main application to streamline extension.

  2. Routing
    Routing can be a tricky business, so using attribute routing might ease some of the burden of fighting with route tables or conflicting controller names.

  3. Build Events
    Build events are just one simple approach to actually getting the necessary .dll files from your custom project into your main application so they can be used.

Taking Advantage of Inheritance

If you actually want to extend the functionality of an existing controller, then inheritance might be the way to go. Inheriting from the controller within your main application will allow you to take advantage of any existing attributes, overridden methods, or underlying base controllers that might already place.

You'll just want to add a reference to your ProjectA project in ProjectB and then target the appropriate controller you wish to inherit from, 

  1. // Other references omitted for brevity  
  2. using ProjectA.Controllers;  
  3.   
  4. namespace ProjectB.Controllers    
  5. {  
  6.     public class CustomFooController : FooController  
  7.     {  
  8.         public ActionResult Bar()  
  9.         {  
  10.             return Content("Bar");  
  11.         }  
  12.     }  
  13. }   

Leverage Attribute Routing

Doing this kind of thing can get a bit dicey with regards to routing. When you attempt to create this new controller within your current application, it'll attempt to use the existing routes defined in that application, which can lead to naming conflicts and ambiguity if you aren't careful.

Based on the code provided, this means you could easily access /CustomFoo/Bar, but not /Foo/Bar like you might prefer. Thankfully, attribute routing can help with this.

Simply decorate your custom controller action with a [Route] attribute that indicates how you wish to access it, 

  1. [Route("Foo/Bar")]  
  2. public ActionResult Bar()    
  3. {  
  4.      return new Content("Bar");  
  5. }   

This will let MVC know to use map this action to the Foo/Bar URL before taking a look at some of the other routes. In order for attribute routing to work as expected, you'll need to ensure to call the MapMvcAttributeRoutes() method within the RouteConfig.cs file of your main application, 

  1. public static void RegisterRoutes(RouteCollection routes)    
  2. {  
  3.     // This is important to write up your Route Attributes  
  4.     routes.MapMvcAttributeRoutes();  
  5.   
  6.     // Route declarations omitted for brevity  
  7. }   

Note

If you were extending a controller that was present within an MVC Area, you would do the same thing within the RegisterArea() method in your AreaRegistration.cs file, 

  1. public override void RegisterArea(AreaRegistrationContext context)    
  2. {  
  3.     // Wire up any attribute based routing  
  4.     context.Routes.MapMvcAttributeRoutes();  
  5.   
  6.     // Area routing omitted for brevity  
  7. }   

Properly Scoping Routes

One additional change that will need to be made within your main application will be to prioritize routes within its own namespace. The MapRoute() method supports an overload to handle this via the namespaces parameter.

Set the namespaces parameter to point to the namespaces that you wish to prioritize, namely those in your main application (i.e. "ProjectA.Controllers"). 

  1. routes.MapRoute(    
  2.    name: "Default",  
  3.    url: "{controller}/{action}/{id}",  
  4.    defaults: new { controller = "Foo", action = "Index", id = UrlParameter.Optional },  
  5.    // This will prioritize routes within your main application  
  6.    namespaces: new[] { "ProjectA.Controllers"}  
  7. );   

Putting It All Together

At this point, code-wise, everything should be in place. The only thing left to do is actually get the code from your ProjectB into ProjectA so that you can run it.

There are a variety of ways to handle this and configure this entire process, but a very simple one might be through a Build Event. Build Events allow you to execute a bit of code during various stages of the build process.

We are interested in defining a Post-Build event that will move our ProjectB.dll file into the bin directory of ProjectA, which might look like this,

xcopy /E /Y /S "$(ProjectName).dll" "$(SolutionDir)\ProjectA\Bin\"

This would be defined under ProjectB > Properties > Build Events > Post-Build Event Command Line as seen below,



Now if you perform a Rebuild Solution, you should see that all of the proper files have been added to your bin directory as expected,


Now if you were to navigate to /Foo within your application, you would see the content that you might expect,



And likewise, /Foo/Bar should now hit your custom code and do exactly what you'd imagine,


Similar Articles