asp.net-mvc - 如何将用户从ASP.MVC 3操作筛选器重定向到另一个Controller Action?

标签 asp.net-mvc asp.net-mvc-3

在构建自定义ASP.MVC 3操作筛选器时,如果测试失败,应如何将用户重定向到另一个操作?我想传递原始的Action,以便在用户输入缺少的首选项后可以重定向回原始页面。

在 Controller 中:

[FooRequired]
public ActionResult Index()
{
    // do something that requires foo
}

在自定义过滤器类中:
// 1. Do I need to inherit ActionFilterAttribute or implement IActionFilter?
public class FooRequired : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (TestForFoo() == false)
        {
            // 2. How do I get the current called action?

            // 3. How do I redirect to a different action,
            // and pass along current action so that I can
            // redirect back here afterwards?
        }

        // 4. Do I need to call this? (I saw this part in an example)
        base.OnActionExecuting(filterContext);            
    }
}

我正在寻找一个简单的ASP.MVC 3过滤器示例。到目前为止,我的搜索导致出现了Ruby on Rails示例或ASP.MVC筛选器示例,这些示例比我需要的复杂得多。如果这已经被问过了,我深表歉意。

最佳答案

您可以将filterContext.Result设置为RedirectToRouteResult:

filterContext.Result = new RedirectToRouteResult(...);

关于asp.net-mvc - 如何将用户从ASP.MVC 3操作筛选器重定向到另一个Controller Action?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5904976/

相关文章:

c# - 使用 Flags 枚举时如何减少 ASP.NET MVC View 中的代码重复

c# - 如何将对象包装在动态对象中?

oracle - 在 MVC3 应用程序中使用带有表单例份验证的 Oracle 数据库

asp.net-mvc-3 - MVC3下拉列表未选择所选项目

c# - ASP.Net MVC 将项目添加到绑定(bind)的下拉列表

c# - System.MethodAccessException、CAS 和 Visual Studio 调试器

asp.net-mvc - Controller 和 View 中的子文件夹

c# - 如何使用 LINQ 将查询结果返回到字典

jquery - MVC 贡献网格

c# - 如何在模型绑定(bind)期间忽略 HttpRequestValidationException 和自己编码的 HTML?