Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于MVC控制器的自定义400重定向_C#_Asp.net Mvc - Fatal编程技术网

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

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

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


假设我有一个管理员控制器和一个帐户控制器,每个控制器都有一个自定义属性。有两种不同的登录表单,我需要管理员控制器在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
    }
}