C# 基于MVC控制器的自定义400重定向
有没有办法在每个控制器的基础上覆盖默认的400重定向(在web.config中设置)C# 基于MVC控制器的自定义400重定向,c#,asp.net-mvc,C#,Asp.net Mvc,有没有办法在每个控制器的基础上覆盖默认的400重定向(在web.config中设置) 假设我有一个管理员控制器和一个帐户控制器,每个控制器都有一个自定义属性。有两种不同的登录表单,我需要管理员控制器在AdminAuthorize属性为false时重定向到它。您可以始终实现自己的: [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)] pu
假设我有一个管理员控制器和一个帐户控制器,每个控制器都有一个自定义属性。有两种不同的登录表单,我需要管理员控制器在AdminAuthorize属性为false时重定向到它。您可以始终实现自己的:
[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
}
}