Part 77 Custom action filters in asp net mvc

kudvenkat
kudvenkat
176.5 هزار بار بازدید - 11 سال پیش - Link for code samples used
Link for code samples used in the demo
http://csharp-video-tutorials.blogspo...

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
@aarvikitchen5572

Link for csharp, asp.net, ado.net, dotnet basics, mvc and sql server video tutorial playlists
kudvenkat

In this video, we will discuss creating custom action filters in asp.net mvc.

Actions are public methods in a controller. Action filters are attributes, that can be applied either on a controller or on a controller action method, which allow us to add pre and post processing logic to the action methods. So, in simple terms an action filter allow us to execute some custom code, either, just before an action method is executed or immediately after an action method completes execution. We have discussed some of the built-in action filters in the previous sessions of this video series.
Part 70 - Authorize attribute
Part 72 - HandleError attribute
Part 73 - OutputCache attribute
Part 75 - RequireHttps attribute
Part 76 - ValidateInput attribute

Now let's discuss, creating a custom action filter. The custom action filter that we are going to build, should log the following information to a text file.
1. The name of the controller
2. The name of the action method
3. Execution time
4. If there is an exception, log the exception message and the time of the exception.

There are 4 types of filters in asp.net mvc.
1. Authorization filters - Implements IAuthorizationFilter. Examples include AuthorizeAttribute and RequireHttpsAttribute. These filters run before any other filter.
2. Action filters - Implement IActionFilter
3. Result filters - Implement IResultFilter. Examples include OutputCacheAttribute.
4. Exception filters - Implement IExceptionFilter. Examples include HandleErrorAttribute.

For detailed explanation of these attributes, please refer to the following MSDN link
http://msdn.microsoft.com/en-us/libra...

Step 1: Create an asp.net mvc 4 application using "Empty" template

Step 2: Right click on the project name in solution explorer and add "Data" folder. Add a text file to this folder and name it Data.txt

Step 3: Right click on the project name in solution explorer and add "Common" folder. Add a class file to this folder and name it "TrackExecutionTime.cs". Copy and paste the following code. Notice that our custom filter "TrackExecutionTime" inherits from ActionFilterAttribute and IExceptionFilter. ActionFilterAttribute class implements IActionFilter and IResultFilter.

Please refer to my blog, for the code associated with this video, using the link below.

Step 4: Add a HomeController. Copy and paste the following code.
public class HomeController : Controller
{
   [TrackExecutionTime]
   public string Index()
   {
       return "Index Action Invoked";
   }

   [TrackExecutionTime]
   public string Welcome()
   {
       throw new Exception("Exception ocuured");
   }
}

Please Note: TrackExecutionTime class is present in MVCDemo.Common namespace.

Build the application and navigate to /Home/Index and then to /Home/Welcome. Notice that the execution times and the exception details are logged in the text file.

Please make sure to replace [ with LESSTHAN and ] with GREATERTHAN symbol.
11 سال پیش در تاریخ 1392/05/23 منتشر شده است.
176,516 بـار بازدید شده
... بیشتر