asp.net-mvc 在asp.net mvc 动作过滤器中重定向到指定的控制器和动作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1490401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:52:48  来源:igfitidea点击:

Redirecting to specified controller and action in asp.net mvc action filter

asp.net-mvcredirectaction-filter

提问by Nick Larsen

I have written an action filter which detects a new session and attempts to redirect the user to a page informing them that this has happened. The only problem is I can not figure out how to make it redirect to a controller/action combo in an action filter. I can instead only figure out how to redirect to a specified url. Is there a direct way to redirect to a controller/action combo in an action filter in mvc2?

我编写了一个动作过滤器,它检测新会话并尝试将用户重定向到一个页面,通知他们发生了这种情况。唯一的问题是我不知道如何让它重定向到动作过滤器中的控制器/动作组合。我只能弄清楚如何重定向到指定的 url。有没有直接的方法可以重定向到 mvc2 中动作过滤器中的控制器/动作组合?

回答by Richard Garside

Rather than getting a reference to HttpContent and redirecting directly in the ActionFilter you can set the Result of the filter context to be a RedirectToRouteResult. It's a bit cleaner and better for testing.

您可以将过滤器上下文的 Result 设置为 RedirectToRouteResult,而不是获取对 HttpContent 的引用并直接在 ActionFilter 中重定向。它更干净,更适合测试。

Like this:

像这样:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if(something)
    {
        filterContext.Result = new RedirectToRouteResult(
            new RouteValueDictionary {{ "Controller", "YourController" },
                                      { "Action", "YourAction" } });
    }

    base.OnActionExecuting(filterContext);
}

回答by Nick Larsen

EDIT: The original question was about how to detect session logout, and then automatically redirect to a specified controller and action. The question proved much more useful as it's current form however.

编辑:最初的问题是关于如何检测会话注销,然后自动重定向到指定的控制器和操作。然而,这个问题被证明更有用,因为它是当前的形式。



I ended up using a combination of items to achieve this goal.

我最终使用了项目的组合来实现这个目标。

First is the session expire filter found here. Then I wanted someway to specify the controller/action combo to get a redirect URL, which I found plenty of examples of here. In the end I came up with this:

首先是在此处找到的会话过期过滤器。然后我想以某种方式指定控制器/操作组合以获取重定向 URL,我在这里找到了很多示例。最后我想出了这个:

public class SessionExpireFilterAttribute : ActionFilterAttribute
{
    public String RedirectController { get; set; }
    public String RedirectAction { get; set; }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext ctx = HttpContext.Current;

        if (ctx.Session != null)
        {
            if (ctx.Session.IsNewSession)
            {
                string sessionCookie = ctx.Request.Headers["Cookie"];
                if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))
                {
                    UrlHelper helper = new UrlHelper(filterContext.RequestContext);
                    String url = helper.Action(this.RedirectAction, this.RedirectController);
                    ctx.Response.Redirect(url);
                }
            }
        }

        base.OnActionExecuting(filterContext);
    }
}

回答by Robert Harvey

Call RedirectToActionusing this overload:

使用此重载调用RedirectToAction

protected internal RedirectToRouteResult RedirectToAction(
    string actionName,
    RouteValueDictionary routeValues
)

In Action Filters, the story is a little different. For a good example, see here:

在动作过滤器中,故事有点不同。有关一个很好的示例,请参见此处:

http://www.dotnetspider.com/resources/29440-ASP-NET-MVC-Action-filters.aspx

http://www.dotnetspider.com/resources/29440-ASP-NET-MVC-Action-filters.aspx