Action Filters with ASP.Net MVC

Hi Friends,

In this section am going to discuss brief about Action Filters and how this is applied in the ASP.Net MVC application. But, before jumping into the details of Action Filters, let’s look @ the definition of Action Filters:- “Action filters are the components that you want to apply to logics which are going to be used across multiple controllers. So, the significance of this, it actually prevents duplication of code inside each controller. ”

There, could be as many

Name Description
OutPut Cache Cache the Output of a controller
Authorize Technique to force only authentic user to login
ValidateAntiForgeryToken Prevents cross site forgeries
HandleError In case of unhandled exception, view can be specified
Validate Input Dangerous one, turn off request validation and allow any input

Now, in case of OutPut cache attribute,

This tells the run time that it’s allowed to cache the final output of some action and use it again till the specified time. when you apply these attributes to few places, you can dramatically increase the performance of the application. Another example could be authorize attribute, it’s job is to check whether authentic user is logged or not, this is many times tied up with roles as well.

Now, let’s look at the small example below for Authorize attribute

[Authorize]
[HttpGet]
public ActionResult Search(string name=”Titanic”)
{
var message = Server.HtmlEncode(name);
return Content(message);
}

Now, when i build and invoke the below url, it will

/Movie/Titanic

it will produce the login screen as shown below:-action filters

so, this is the action filter in brief. For the deep dive and know more about the Actions Filters in action subscribe to ONLINE ASP.Net MVC workshop. Till then stay tuned and happy coding.

Thanks,
Rahul

1,342 total views, 2 views today