From the course: ASP.NET MVC: Building for Productivity and Maintainability

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Reducing duplicate code with custom action filters

Reducing duplicate code with custom action filters

From the course: ASP.NET MVC: Building for Productivity and Maintainability

Start my 1-month free trial

Reducing duplicate code with custom action filters

- [Instructor] In an earlier video, I showed you how to have a controller action respond with either a full page view or a partial view, depending on whether the request was an Ajax request. In order to do this, I added just a few lines of code. This works great, and is so simple that you could even just copy and paste this code to all of your other controller actions to enable the same functionality for them too. However, if you find yourself wanting to do that, to copy and paste some snippet of code in a whole bunch of controller actions, you should instead consider creating a custom action filter. You're already familiar with ASP.NET MVC action filters, even if you weren't aware that you were using them. For instance, there're the attributes like Authorize or HttpGet and HttpPost. And because they allow you to introduce behavior simply by adorning a class or method with an attribute, action filters are perhaps the most powerful feature available in the framework. What's more, it's…

Contents