Filter overriding in ASP.NET MVC 5.0


In this article we are going to look at one of the new features of ASP.NET MVC 5.0: Overriding Filter. We will look at a simple example of how to override the global filter and apply it to the action methods in the HomeController class.

In this article we are going to focus on how to override filters in MVC 5.

Global Filter: It is an action filter which is automatically added to the list of filters by the default action invoker before it invokes any action methods.

In case of global action filter, code is written once and attached to all the controllers in a single step.

To register the filter as a global filter in the application add it to the filters collection in the RegisterGlobalFilters method of the FilterConfig class in the App_Start folder:


public class MvcApplicationDemo : HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
//Add custom filters
}
...
}

Create a New ASP.NET Web Application in Visual Studio 2013. Then select MVC using .NET Framework 4.5.

Now update the HomeController class in the Controllers folder as shown below:


[Authorize(Users="test@gmail.com")]
public class HomeController : Controller
{
[OverrideActionFilters]
public ActionResult Index()
{
return View();
}

[OverrideAuthorization]
[Authorize(Users="abc@hotmail.com")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";

return View();
}

public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";

return View();
}
}


Now browse the application and Register using the below two users and notice the following about the OverrideFilter attribute.
test@gmail.com
abc@hotmail.com

The important points to note in the above code is:

1. Authorize filter is applied to the entire HomeController.
2. contact action method is accessible by only the 'test@gmail.com' user.
3. About action method overrides the Authorization by using the OverrideAuthorization filter attribute and is accessible by only the 'abc@hotmail.com' user.
4. OverrideActionFilters attribute is used to override the global action filter in the About action method.
5. OverrideActionFilters implements the IOverrideFilter interface.
6. 'abc@hotmail.com' user can access only the About action method. This user cannot access both the Index and Contact action methods.
7. 'test@gmail.com' user can access only the Home and Contact action methods but not the About action method.
8. 'OverrideAuthorization' filter attribute overrides the authorization attribute and authorizes only the user 'abc@hotmail.com' to access the About method.


Article by Vaishali Jain
Miss. Jain Microsoft Certified Technology Specialist in .Net(Windows and Web Based application development)

Follow Vaishali Jain or read 127 articles authored by Vaishali Jain

Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: