【问题标题】:Custom 400 redirect based on MVC Controller基于 MVC 控制器的自定义 400 重定向
【发布时间】:2010-06-22 04:36:59
【问题描述】:

有没有办法在每个控制器的基础上覆盖默认的 400 重定向(在 web.config 中设置)?

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

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-09-28
      • 2014-07-24
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 1970-01-01
      相关资源
      最近更新 更多