开发者

Custom 400 redirect based on MVC Controller

Is there a way to override the default 400 redirect (set in web.config) on a per-controller basis?

Say I have an Admin controller and an Account controller, each has a custom AuthorizeAttribute. There are two different login forms, and 开发者_JAVA技巧I need the admin controller to redirect to it's when AdminAuthorize attribute is false.


You could always implement your own 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
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜