内GlobalActionFilter重定向失败

内GlobalActionFilter重定向失败

本文介绍了内GlobalActionFilter重定向失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建重定向登录用户到我的网站上指定的网页GlobalActionFilter。这是关系到一个previous问题,我问关于全球重定向(Need对ASP.NET MVC网站的多个不同的条件)

I've created a GlobalActionFilter to redirect logged in users to designated pages on my site. This is related to a previous question that I asked regarding global redirects (Need recommendation for global, targeted redirects on ASP.NET MVC site for multiple differing conditions)

在此GlobalActionFilter code正在执行,但它不是重定向浏览器。我在想什么?下面是从Global.asax.cs中的code。该ForceExternalUserCompletion()是我的自定义的全球行动的过滤器。

Code in this GlobalActionFilter is being executed, but it's not redirecting the browser. What am I missing? Here is the code from the Global.asax.cs. The ForceExternalUserCompletion() is my custom global action filter.

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);

    GlobalFilters.Filters.Add(new Filters.ForceExternalUserCompletion());
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    AuthConfig.RegisterAuth();
}

下面是从我的GlobalActionFilter code。

Here is the code from my GlobalActionFilter.

/// <summary>
/// Direct a logged in ExternalUser to complete unfinished items.  Hierarchy is as follows:
/// 1. Security Questions
/// 2. Password Reset
/// </summary>
public class ForceExternalUserCompletion : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        base.OnResultExecuting(filterContext);

        HttpCookie externalUserCookie = filterContext.HttpContext.Request.Cookies["ExternalUser"];
        if (externalUserCookie != null)
        {
            // 1. Security Questions
            if (externalUserCookie["ForceQA"] == "1")
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary
                    {
                        { "controller", "MyInfo" },
                        { "action", "ChangeSecurityQuestions" }
                    });

                return;
            }

            // 2. Password Reset
            if (externalUserCookie["ForcePass"] == "1")
            {
                filterContext.Result = new RedirectToRouteResult(
                    new RouteValueDictionary
                    {
                        { "controller", "MyInfo" },
                        { "action", "ChangePassword" }
                    });

                return;
            }
        }
    }
}

通过调试器,我可以看到这个动作过滤器中的code正在执行。里面externalUserCookie [ForceQA] ==1运行code座后,我希望被重定向到MyInfo的/ ChangeSecurityQuestions查看,但没有发生。并没有异常被抛出。

Via debugger I can see the code within this action filter is being executed. After the code block inside externalUserCookie["ForceQA"] == "1" runs, I expect to be redirected to the MyInfo/ChangeSecurityQuestions view, but that's not happening. And no exception is being thrown.

时使用filterContext.Result正确的做法,如果是的话是有code的一些额外的行,我需要补充,所以这成功的重定向浏览器?

Is using "filterContext.Result" the correct approach, and if so is there some additional line of code that I need to add so that this will successfully redirect the browser?

感谢您的帮助。

===编辑2015年3月13日在10:33中央===

=== Edit 3/13/2015 at 10:33 AM Central ===

感谢大家的帮助和想法。现在我怀疑的东西是错误与我的语法RedirectToRouteResult。如果我替换这一行code,浏览器重定向到雅虎。

Thanks everyone for your help and ideas. Right now I suspect something is wrong with my syntax for the RedirectToRouteResult. If I replace that with this line of code, the browser redirects to Yahoo.

filterContext.RequestContext.HttpContext.Response.Redirect("http://www.yahoo.com");

===编辑2015年3月13日上午11:00中央===

=== Edit 3/13/2015 at 11:00 AM Central ===

由于安杰洛和夜鹰。下面是我最终应用修复。

Thanks Angelo and NightOwl. Below is the fix that I ended up applying.

public class ForceExternalUserCompletion : ActionFilterAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        base.OnResultExecuting(filterContext);

        HttpCookie externalUserCookie = filterContext.HttpContext.Request.Cookies["ExternalUser"];
        if (externalUserCookie != null)
        {
            string controller = filterContext.RequestContext.RouteData.Values["controller"].ToString();
            string action = filterContext.RequestContext.RouteData.Values["action"].ToString();

            // 1. Security Questions
            if (externalUserCookie["ForceQA"] == "1")
            {
                // Prevent a perpetual redirect.
                if (controller != "MyInfo" && action != "ChangeSecurityQuestions")
                {
                    filterContext.RequestContext.HttpContext.Response.RedirectToRoute(
                        new RouteValueDictionary
                    {
                        { "controller", "MyInfo" },
                        { "action", "ChangeSecurityQuestions" }
                    }
                    );
                }

                return;
            }

            // 2. Password Reset
            if (externalUserCookie["ForcePass"] == "1")
            {
                // Prevent a perpetual redirect.
                if (controller != "MyInfo" && action != "ChangePassword")
                {
                    filterContext.RequestContext.HttpContext.Response.RedirectToRoute(
                        new RouteValueDictionary
                    {
                        { "controller", "MyInfo" },
                        { "action", "ChangePassword" }
                    }
                    );
                }

                return;
            }
        }
    }
}

不同的是,该固定code使用RedirectToRoute代替RedirectToRouteResult。这也检查当前控制器和动作,这样,当在浏览器上的目标网页之一的土地,它通过一个重定向循环不循环。我会有点打扫一下,但本质上这是固定的。谢谢你。

The difference is that this fixed code uses RedirectToRoute instead of RedirectToRouteResult. And this also checks the current controller and action so that when the browser lands on one of the target pages, it doesn't cycle through a redirect loop. I'll clean this up a bit, but essentially this is fixed. Thanks.

推荐答案

您调用 RedirectToRouteResult 是可疑的,因为你不叫名字的路线了。我怀疑你没有得到一个路由匹配。

Your call to RedirectToRouteResult is suspicious because you are not calling the route out by name. I suspect that you are not getting a route match.

如果您传递参数给名称参数,你会更容易得到匹配。

If you pass a parameter to the name parameter, you will be more likely to get a match.

public override void OnResultExecuting(ResultExecutingContext filterContext)
{
    HttpCookie externalUserCookie = filterContext.HttpContext.Request.Cookies["ExternalUser"];
    if (externalUserCookie != null)
    {
        // 1. Security Questions
        if (externalUserCookie["ForceQA"] == "1")
        {
            filterContext.Result = new RedirectToRouteResult(
                "Default",
                new RouteValueDictionary
                {
                    { "controller", "MyInfo" },
                    { "action", "ChangeSecurityQuestions" }
                });

            return;
        }

        // 2. Password Reset
        if (externalUserCookie["ForcePass"] == "1")
        {
            filterContext.Result = new RedirectToRouteResult(
                "Default",
                new RouteValueDictionary
                {
                    { "controller", "MyInfo" },
                    { "action", "ChangePassword" }
                });

            return;
        }
    }

    // If we made it here, pass the call on to the action method
    base.OnResultExecuting(filterContext);
}

如果你只需要调用它可能是简单的 RedirectToAction

It might be simpler if you just call RedirectToAction.

filterContext.Result = new RedirectToAction(action: "ChangePassword", controller: "MyInfo");

这篇关于内GlobalActionFilter重定向失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 06:34