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 ...
    MyBase.OnActionExecuting(filterContext)
  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)
    MyBase.OnActionExecuting(filterContext)
  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 http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Cloud-Focused .NET Aspire 9.1 Released

    Along with .NET 10 Preview 1, Microsoft released.NET Aspire 9.1, the latest update to its opinionated, cloud-ready stack for building resilient, observable, and configurable cloud-native applications with .NET.

  • Microsoft Ships First .NET 10 Preview

    Microsoft shipped .NET 10 Preview 1, introducing a raft of improvements and fixes across performance, libraries, and the developer experience.

  • C# Dev Kit Previews .NET Aspire Orchestration

    Microsoft's dev team has been busy updating the C# Dev Kit, a Visual Studio Code extension that enhances the C# development experience by providing tools for managing, debugging, and editing C# projects.

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

Subscribe on YouTube

Upcoming Training Events