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?