You must Sign In to post a response.
  • Category: ASP.Net MVC

    What is difference between action filter and action attributes in MVC?

    Pls explain the following

    1. Action Filters
    2. Action Attributes
    3. Difference between Action filters and Action Attributes.
  • #732959
    The ASP.NET MVC framework allows you to run code both before an action method is called or
    after it has run using action filters. Action filters are applied by using attributes to
    action methods. Action filters are attribute classes that implement the FilterAttribute base class.ASP.NET MVC includes several predefined action filters such as Authorize, OutputCache,
    and HandleError. The Authorize filter allows you to check for security before executing an action
    method. OutputCache allows you to cache the results of an action method.

    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #733070
    hey,

    Action attributes are a way to adnotate classes and properties and methods,
    and
    The ActionFilter is only MVC class to decorate, well, Actions.

    to get more details read this one.
    http://www.codeproject.com/Articles/577776/AplusBeginner-27splusTutorialplusforplusUnderstand

    i hope this help you,

    Thanks,
    ketan

  • #765285
    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());
    }
    Here I would like to share something useful about freshers who wanted to learn any technology or career growth, please visit this website.
    http://www.traininginsholinganallur.in/big-data-analytics-training-in-chennai.html
    http://www.traininginsholinganallur.in/primavera-training-in-chennai.html
    http://www.traininginsholinganallur.in/qtp-training-in-chennai.html

  • #765490
    The ASP.NET MVC framework includes action filters

    OutputCache – This action filter caches the output of a controller action for a specified amount of time

    HandleError – This action filter handles errors raised when a controller action executes.

    Authorize – This action filter enables you to restrict access to a particular user or role.

    Regards,
    Dipti Choudhari

  • #765500
    Action Filters are additional attributes that can be applied to either a controller section or the entire controller to modify the way in which action is executed. These attributes are special .NET classes derived from System.Attribute which can be attached to classes, methods, properties and fields.

    ASP.NET MVC provides following action filters:

    Output Cache: This action filter caches the output of a controller action for a specified amount of time.
    Handle Error: This action filter handles errors raised when a controller action executes.
    Authorize: This action filter enables you to restrict access to a particular user or role.
    Now we will see the code example to apply these filters on an example controller ActionFilterDemoController. (ActionFilterDemoController is just used as an example. You can use these filters on any of your controllers.)

    Output Cache
    E.g.: Specifies the return value to be cached for 10 seconds.

    public class ActionFilterDemoController : Controller
    {
    [HttpGet]
    OutputCache(Duration = 10)]
    public string Index()
    {
    return DateTime.Now.ToString("T");

    }
    }
    Handle Error
    E.g.: Redirect application to a custom error page when an error is triggered by controller

    [HandleError]
    public class ActionFilterDemoController : Controller
    {
    public ActionResult Index()
    {
    throw new NullReferenceException();
    }

    public ActionResult About()
    {
    return View();
    }
    }
    With the above code, if any error happens during the action execution, it will find a view named Error in the Views folder and render that page to the user.

    Authorize
    E.g.: Allowing only authorized users to log in the application

    public class ActionFilterDemoController: Controller
    {
    [Authorize]
    public ActionResult Index()
    {
    ViewBag.Message = "This can be viewed only by authenticated users only";
    return View();
    }

    [Authorize(Roles="admin")]
    public ActionResult AdminIndex()
    {
    ViewBag.Message = "This can be viewed only by users in Admin role only";
    return View();
    }
    }

  • #765501
    Attributes : =
    They are used in the controller for example for authentication

    [ChessAuth(EnumPermission = PermissionsEnum.CanReadDashboard, TypePermission = PermissionType.Project)]
    public ActionResult Dashboard(int organisationId, int projectId = 0)
    {
    var u = GetUser();
    FillViewBagIds(u, projectId);
    ViewBag.Permissions = GetPermissions(u, projectId);
    return View();
    }
    Action Filters : =
    They are like attributes in controller they actually are somewhat the same

    [OutputCache(Duration=10)]
    public string Index()
    {
    return DateTime.Now.ToString("T");

    }
    so to be short

    Attributes/Action filters => same stuff


  • Sign In to post your comments