Tips and Tricks

Implementing Default Behavior in an ASP.NET MVC Controller

If there's something you want to do every time an Action method is called in a Controller, there's an easy way to achieve that. It may even be a best practice.

Every Controller class has a method called OnActionExecuting that runs before any Action method is called (there's also an OnActionExecuted that runs after any Action method). If you wanted to implement some logging code to keep track of a Controller's behavior, OnActionExecuting would be a great place to put your logging code:

Public Class HomeController
  Inherits System.Web.Mvc.Controller

  Protected Overrides Sub OnActionExecuting(filterContext As ActionExecutingContext)
    '... logging code ...
  End Sub

Of course, if you wanted to apply this code on only some methods in some controllers, you could create an Action Filter. But what if you wanted to apply this code to every action method in every controller? You have two choices.

You could, of course, create an Action Filter and apply it through the GlobalFilterCollection in the FilterConfig class in your application's App_Start folder. Or you could create a base Controller class to hold the method:

Public Class ControllerBase
  Inherits System.Web.Mvc.Controller

  Protected Overrides Sub OnActionExecuting(filterContext As ActionExecutingContext)
  End Sub

End Class

And then have all your other controllers inherit from your new base Controller class:

Public Class HomeController
  Inherits ControllerBase

Personally, I think it's a good idea to have your own base class for your Controllers. You may not need it yet, but when you do have some code you want to share with all your Controllers, you'll be glad that base class exists.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at

comments powered by Disqus


  • Vendors Update Controls for .NET Core 3.1, Blazor

    This week saw two third-party vendors of dev tools -- UX and UI toolkits and controls -- release new offerings that include support for two of Microsoft's main open source frameworks, the cross-platform .NET Core 3.1 and Blazor, which allows for creating browser-based web applications with C# instead of JavaScript.

  • C++ Is Focus of New Visual Studio 2019 v16.7 Preview 2

    C++ development is a focus point of the new Visual Studio 2019 v16.7 Preview 2, featuring a slew of tweaks and improvements touching upon remote SSH connections, IntelliSense support and more.

  • Clustering Non-Numeric Data Using C#

    Clustering non-numeric -- or categorial -- data is surprisingly difficult, but it's explained here by resident data scientist Dr. James McCaffrey of Microsoft Research, who provides all the code you need for a complete system using an algorithm based on a metric called category utility (CU), a measure how much information you gain by clustering.

  • So What's Up with Microsoft's (and Everyone Else's) Love of Rust?

    Microsoft already stewards several popular programming languages -- C#, TypeScript, F# -- so what's up with its love of Rust, along with the rest of the world?

  • C# Steps Up Programming Language Popularity Ladder

    Microsoft's C# programming language climbed a year-over-year notch on the TIOBE Index, which measures popularity among developers.

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.

Upcoming Events