13

나는 매개 변수를 받아들이려고하는 ActionFilterAttribute을 가졌지 만 그들을 넘겨 줄 수 없다.액션 필터 ActionParameters

내 작업 필터는 다음과 같습니다.

public class PreventAction : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     filterContext.Result = new RedirectResult("Home/Index"); 
    } 
} 

그리고 나는 내 행동을 그렇게 꾸미기도한다.

[PreventAction] 
public ActionResult Ideas() 
{ 
    return View(); 
} 

이제 매개 변수를 추가하여이 필터를 호출 할 수 있습니다.

[PreventAction(myParam1 = "1", myParam2 = "2")] 
public ActionResult Ideas() 
{ 
    return View(); 
} 

누구나 알고 계십니까?

답변

25

PreventAction 클래스의 속성으로 및 MyParam2을 추가하기 만하면됩니다. 매개 변수가 필요하다면 (옵션이 아닌) PreventAction의 생성자에 인수로 추가하십시오.

여기 MSDN의 간단한 특성 클래스 인 quick tutorial이 있습니다.

+1

+1 환상적인! 감사합니다. – griegs

+1

도와 주셔서 감사합니다 :) – womp