代码之家  ›  专栏  ›  技术社区  ›  Jeremy Boyd

基于MVC控制器的自定义400重定向

  •  0
  • Jeremy Boyd  · 技术社区  · 16 年前

    是否有方法基于每个控制器重写默认的400重定向(在web.config中设置)?

    假设我有一个管理员控制器和一个帐户控制器,每个都有一个自定义的authorizeattribute。有两种不同的登录表单,当adminauthorize属性为false时,我需要管理员控制器重定向到它。

    1 回复  |  直到 16 年前
        1
  •  1
  •   Darin Dimitrov    16 年前

    你总是可以实现你自己的 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
        }
    }