Asp.net mvc 如何将用户从ASP.MVC 3操作筛选器重定向到另一个控制器操作?
在构建自定义ASP.MVC 3操作筛选器时,如果测试失败,如何将用户重定向到其他操作?我希望传递原始操作,以便在用户输入缺少的首选项后重定向回原始页面 在控制器中:Asp.net mvc 如何将用户从ASP.MVC 3操作筛选器重定向到另一个控制器操作?,asp.net-mvc,asp.net-mvc-3,Asp.net Mvc,Asp.net Mvc 3,在构建自定义ASP.MVC 3操作筛选器时,如果测试失败,如何将用户重定向到其他操作?我希望传递原始操作,以便在用户输入缺少的首选项后重定向回原始页面 在控制器中: [FooRequired] public ActionResult Index() { // do something that requires foo } 在自定义筛选器类中: // 1. Do I need to inherit ActionFilterAttribute or implement IActionFi
[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过滤器示例。到目前为止,我的搜索结果显示RubyonRails示例或ASP.MVC过滤器示例比我需要的复杂得多。如果以前有人问过,我深表歉意。您可以将
过滤器Context.Result
设置为重定向路由结果
:
filterContext.Result = new RedirectToRouteResult(...);
下面是一个使用我自己的重定向过滤器的小代码示例:
public class PrelaunchModeAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//If we're not actively directing traffic to the site...
if (ConfigurationManager.AppSettings["PrelaunchMode"].Equals("true"))
{
var routeDictionary = new RouteValueDictionary {{"action", "Index"}, {"controller", "ComingSoon"}};
filterContext.Result = new RedirectToRouteResult(routeDictionary);
}
}
}
如果你想拦截路线,你可以从
使用该RoutedData成员,您可以获得原始路线:
var currentRoute = filterContext.RouteData.Route;
等等。。。这有助于回答您的问题吗?这可能会重复回答我的部分问题。在我询问之前,我在搜索StackOverflow时没有得到这个。仍然想知道如何传递现有的动作。@Talljoe和mods:更像是一个可能的复制品,对我来说很有用,唯一的例外是我仍在试图找出如何传递当前路径,以便知道哪个动作被中断。我猜这是缺少的成分:你是如何使用的filterContext.RouteData.Route?我尝试了新的RedirectToRouteResult(filterContext.RouteData.Route)。如何将数据传递给“索引”操作?