سؤال

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.

هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
 var model = filterContext.ActionParameters.SingleOrDefault(ap => ap.Key == "uid").Value;


       if (model != null)
       {
          dosomething(id, name, uid)
       }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top