在构建自定义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/