What is different between attribute filter and action filter? [Resolved]

Posted by Kumarkrishna184 under ASP.NET MVC on 3/9/2016 | Points: 10 | Views : 1303 | Status : [Member] | Replies : 2
What is different between attribute filter and action filter?

Thanks and Regards,
Krishna Kumar



Responses

Posted by: Rajnilari2015 on: 3/11/2016 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
1
Down

Resolved
@Kumarkrishna184 Sir,


An action filter (can also be call as attribute filter) is an attribute which is applicable for a controller action. E.g. the below code exposes an action named Index() which returns the current time. This action is decorated with the OutputCache action filter.This filter causes the value returned by the action to be cached for 20 seconds.


using System;
using System.Web.Mvc;

namespace Sample.Example
{
public class TestController : Controller
{
[OutputCache(Duration=20)]
public string Index()
{
return "The time is " + DateTime.Now;

}
}
}


We can however create custom Filters as below

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)] 
public class TestProfilerAttribute : FilterAttribute, IActionFilter
{
public void OnActionExecuted(ActionExecutedContext filterContext)
{
throw new NotImplementedException();
}
public void OnActionExecuting(ActionExecutingContext filterContext)
{
throw new NotImplementedException();
}
}


Hope this helps

--
Thanks & Regards,
RNA Team

Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Yasminpriya on: 3/26/2016 [Member] Starter | Points: 25

Up
0
Down
In most cases, the attributes are used to describe metadata about methods/classes/etc. For example, there's the Serializable attribute to indicate that a class can be serialized, TestClass attribute to mark a class as a test, and the Obsolete attribute to mark something as obsolete. Reflection is used to extract this information by a process that wants to use them. It's covered well in this question about attributes.

The filter attributes in MVC, such as AuthorizeAttribute, conveys extra information similar to other attributes -- a controller method or class decorated by AuthorizeAttribute indicates that authorization is required when used by MVC. But unlike some other attributes, the filter attributes themselves contain logic to carry out the actual function -- the AuthorizeAttribute derives from Attribute (via FilterAttribute) and also implements IAuthorizationFilter. When MVC finds a controller class decorated by AuthorizeAttribute, it will call AuthorizeAttribute.OnAuthorization() method in to carry out the authorization. Also, when you are specifying global filters, you add the attribute class itself to the filters list, which can be a little bit confusing, but that's how it works:

void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new AuthorizeAttribute());
}
Regards..


Kumarkrishna184, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response