如何将程序流重定向到控制器操作。我想在Global.asax.cs中模拟MVC的RedirectToAction(“ActionName”,“ControllerName”,路由值)调用。我该怎么办?
答案 0 :(得分:4)
如果您使用的是MVC 3,我建议使用writing your own actionfilter,然后您可以apply globally。
一个小代码示例:
public class HandleSessionTimeoutAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(FilterExecutingContext filterContext)
{
// Do whatever it is you want to do here.
// The controller and request contexts, along with a whole lot of other
// stuff, is available on the filter context.
}
}
然后在Global.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Register global filter
GlobalFilters.Filters.Add(new HandleSessionTimeoutAttribute());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}