왜 ActionFilterAttribute redirect 후 조치 코드는 실행하는가?
-
12-09-2020 - |
문제
내가 사용하려 ActionFilterAttribute 리디렉션하려면 사용자는 로그인을 하지 않았습니다.하지만 내가 리다이렉션이 일 그것을 리디렉션을 하지만 그것은 모두의 내 ActionResult 코드를 처음이다.
어떤 아이디어를 제시하지 않는 이유는 명예의 진정한 국고 결국 대답하시겠습니까?
HttpContext.Current.Response.Redirect("~/Logon",true);
여기에 내 ActionResult:
[RequireLoggedIn]
public ActionResult Create()
{
return View(_MessageService.GetAllMessageCategories());
}
이것은 나의 속성:
public class RequireLoggedIn : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (Membership.GetUser() == null)
{
//Not logged in so back to the logon page.
HttpContext.Current.Response.Redirect("~/Logon",true);
HttpContext.Current.Response.End();
}
}
}
해결책
을 설정해야 합 filterContext.Result
가 null 이 아닌을 피하는 다운스트림 처리,그렇지 않으면 MVC 지 않는 것을 알고 있는 단락.가장 간단한 접근법이 될 것이라고:
public override void OnActionExecuting(ActionExecutingContext filterContext) {
if (Membership.GetUser() == null) {
filterContext.Result = new RedirectResult("~/Logon");
}
}
제휴하지 않습니다 StackOverflow