重定向到在asp.net的MVC行为过滤器指定的控制器和行动 [英] Redirecting to specified controller and action in asp.net mvc action filter

查看:616
本文介绍了重定向到在asp.net的MVC行为过滤器指定的控制器和行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一个动作过滤器检测一个新的会话,并试图将用户重定向到告知他们,这已经发生了一个页面。唯一的问题是我无法弄清楚如何使它重定向到一个控制器/动作组合在一个动作的过滤器。我不是只有弄清楚如何重定向到指定的URL。有没有重定向到一个控制器/动作组合在MVC2的动作过滤器直接的方式?

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?

推荐答案

而不是前往HttpContent参考,并直接在ActionFilter重定向可以设置过滤器上下文的结果是一个RedirectToRouteResult。这是一个用于测试的位清洁和更好的。

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.

这样的:

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

    base.OnActionExecuting(filterContext);
}

这篇关于重定向到在asp.net的MVC行为过滤器指定的控制器和行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆