Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.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
Asp.net mvc ASP.NET MVC 4在ActionFilterAttribute中重定向响应_Asp.net Mvc_Action Filter - Fatal编程技术网

Asp.net mvc ASP.NET MVC 4在ActionFilterAttribute中重定向响应

Asp.net mvc ASP.NET MVC 4在ActionFilterAttribute中重定向响应,asp.net-mvc,action-filter,Asp.net Mvc,Action Filter,所以基本上我有一个自定义的actionfilteratAttribute,在其中我覆盖了一个类似这样的方法 public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext) { bool shouldEnter = true; string redirUrl = "SomeController/SomeAction"; // some checks her

所以基本上我有一个自定义的
actionfilteratAttribute
,在其中我覆盖了一个类似这样的方法

public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
{
    bool shouldEnter = true;
    string redirUrl = "SomeController/SomeAction";
    // some checks here which change the value of shouldEnter;
    if (!shouldEnter)
    {
        filterContext.HttpCOntext.Response.Redirect(redirUrl);
    }
}

这是
ActionFilterAttribute
的一个非常简单的实现。基本上,我检查了一些东西,然后决定是否让他进来。如果我不让他进来,我应该将他重定向到某个URL,我使用上面的
OnActionExecuting
方法实现了这一点。但问题是,在它进入someUrl之前,他请求的操作首先被执行。假设我在
~/SomeController/youshouldnotenthere
请求页面,并且该操作具有该属性。调用了
OnActionExecuting
,我假设会调用
redirl
(在
OnActionExecuting
方法中提到)的相应操作,因为我将
响应重定向到它,但是不,首先调用/SomeController/you的相应操作(但实际上未在浏览器中绘制,但该操作仍会占用时间)并且会调用相应的
redirl
操作(并在浏览器中绘制)。这是某种错误吗?还是应该这样做?无论如何,我能做什么?听起来你在做授权,在这种情况下使用的正确属性是
AuthorizeAttribute

此代码将修复现有属性:

filterContext.Result = new RedirectResult { Url = redirUrl };

那么你想说我不能用ActionFilterAttribute实现我想要的吗?@Dima用重定向现有属性的方法编辑了我的帖子这是
AuthorizeAttribute
存在的一个具体原因。它在任何
ActionFilterAttribute
之前运行,这意味着如果你不要使用它。@erik是正确的,您无法控制类似属性的运行顺序……因此,如果您确实不想执行任何其他处理,除非//此处的某些检查更改了shouldEnter;的值。那么您需要使用authorized属性。尽管如此,上面的简单代码修复将阻止控制器/操作正如您在问题中所解释的那样,受到攻击..作为开发人员,我们应该考虑这些事情;)可能重复的
filterContext.Result=new RedirectResult{Url=redirl}@felickz实际上帮助我写了一个答案,这样我就可以标记它了。