2010-06-21 72 views
0

有沒有一種方法可以在每個控制器的基礎上覆蓋默認的400重定向(在web.config中設置)?基於MVC控制器的自定義400重定向

說我有一個管理控制器和一個帳戶控制器,每個都有一個自定義的AuthorizeAttribute。有兩種不同的登錄表單,當AdminAuthorize屬性爲false時,我需要管理員控制器重定向到它。

回答

1

你總是可以實現自己的authorization attribute

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)] 
public class CustomAuthorizeAttribute : AuthorizeAttribute 
{ 
    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
    { 
     var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName; 
     var actionName = filterContext.ActionDescriptor.ActionName; 
     // TODO: Now that you know the controller name and the action name 
     // act accordingly or call the base method 
    } 
}