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
From the course: ASP.NET MVC: Building for Productivity and Maintainability
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
-
(Locked)
Reducing duplicate code with custom action filters5m 49s
-
(Locked)
Creating explicit contracts with request objects6m 51s
-
(Locked)
Moving business logic from a controller to a service7m 10s
-
(Locked)
Consuming a service from a controller3m 11s
-
(Locked)
Challenge: Creating a model validation action filter2m 7s
-
(Locked)
Solution: Creating a model validation action filter3m 55s
-
(Locked)
-
-
-