Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Take parameter of a method into custom action filter MVC3 asp

I've been working on an application that everytime you register, modify, cancel, delete ,etc... something, a notification has to be sent to a user(if it is configured) so i'm planning to do this by saving a unique identifier of the proccess on the database and then check if the notification is configured with an id, a proccess and the unique identifier and after all this send the notification.

To do this i had this in mind... this on the controller

   [NotificationFilter(id=10,proccess="Excecution") ]
   public Register(Entity entity,Guid uid){

   }

This on the ActionFilter class

public class NotificationFilter : ActionFilterAttribute
{
    public int id{ get; set; }
    public string proccess{ get; set; }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {

         dosomething(id,name,uid)
    }
}

but I want to know if there is a way to get the uid parameter of my Register action into the OnActionExecuted method.

like image 534
Seichi Avatar asked Sep 12 '13 05:09

Seichi


1 Answers

Taking Neel Bhatt answer.. I did something like this to make this work.

public class NotificationFilter : ActionFilterAttribute
{
    public int _id;
    public string _proccess;
    public Guid _uid; 

    public NotificationFilter(int id,string proccess)
    {
        _id= id;
        _proccess = proccess;
    }
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
         var val = filterContext.ActionParameters["uid"];

        _uid = (Guid)val;
    }

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
         var uid = _uid;

         dosomething(id,name,uid)
    }
}

This works for me, and hope could be helpfull to someone else.

like image 152
Seichi Avatar answered Sep 19 '22 12:09

Seichi