2010-08-09 4 views
15

메서드 속성에서 리디렉션을 실행하려고합니다. 내가 방법의 실행을 방지하기 위해 리디렉션을하고 싶습니다 동안ASP.NET MVC가 속성에서 리디렉션합니다.

public class MyAttribute: ActionFilterAttribute { 
    [..] 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     [..] 
     filterContext.HttpContext.Response.Redirect(urlToRedirectTo, true); 
     [..] 

유일한 문제는, 리디렉션이가 붙어 방법이 끝난 후 실행이다 : 작동하는 것 같다.

어떤 도움이 필요합니까? 감사합니다

답변

29

FilterContext.Result에 ActionResult를 할당하여 작업 메서드 실행을 방지 할 수 있습니다. 예를 들어 :

filterContext.Result = new RedirectResult(urlToRedirectTo); 
-2

당신은 추가 할 필요가

filterContext.HttpContext.Response.End(); 

편집 : 당신이 도착 후 ActionResult를 반환하는 경우

이이

filterContext.Result = new RedirectResult(UrlToRedirectTo); 

작동합니다 상단은 바닥이 옳다 잘못 액션 자체를 실행 했으므로 액션을 건너 뜁니다.

+0

나는 그가'HttpResponse.Redirect' 방법의 두 번째 매개 변수로 'TRUE'를 넣어 반응을 종료 생각 .. –

관련 문제