-3

I have checked the answers in here. How to pass parameters to a custom ActionFilter in ASP.NET MVC 2? I want to check a condition in several controllers, based on a unique id that is passing into the controller.

here is the code for actionfilter.

public class AnswerCompletedAttribute : System.Web.Http.Filters.ActionFilterAttribute, IActionFilter
{
    public string p { get; set; }

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        throw new NotImplementedException();
    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (filterContext.ActionParameters.ContainsKey(p))
        {
            var id = filterContext.ActionParameters[p] as Guid?;
            if (Db.IsRespondentAnswerCompleted(id))
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary
                    {
                    { "controller", "Error" },
                    { "action", "NotFound" }
                    });
            }
        }
    }
}

And my code in the controller,

[AnswerCompleted(p = "uniqueId")]
    public ActionResult Index(Guid? uniqueId)
    {

        //do something
        return View();
    }

How should I change it to make it work?

Community
  • 1
  • 1
Wilheim
  • 123
  • 3
  • 13

1 Answers1

0

I found that it is inherited from the wrong class, it should be system.web.mvc.actionfilterattribute instead of System.Web.Http.Filters.ActionFilterAttribute

Wilheim
  • 123
  • 3
  • 13