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,
- namespace ProjectA.Controllers
- {
- // This is the primary controller that we want to extend
- public class FooController : ApplicationController
- {
- public ActionResult Index()
- {
- return Content("Foo");
- }
- }
- }
And you might have a similar class in ProjectB that resembles this,
- namespace ProjectB.Controllers
- {
- // You want to essentially add the Bar action to the existing controller
- public class CustomFooController : FooController
- {
- public ActionResult Bar()
- {
- return Content("Bar");
- }
- }
- }
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,
- Inheritance
The custom controller will inherit from the controller in our main application to streamline extension. - 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. - 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,
- // Other references omitted for brevity
- using ProjectA.Controllers;
- namespace ProjectB.Controllers
- {
- public class CustomFooController : FooController
- {
- public ActionResult Bar()
- {
- return Content("Bar");
- }
- }
- }





kai villarPosted Apr 29, 2021, 5:51 AM
How about in ASP.NET Core MVC ?
Manav PandyaPosted Jan 14, 2017, 8:52 AM
Thanks for sharing sir ....