用法: filterContext.RedirectToAction("Login", "Account");
这是我编写的一个辅助类,其中包含一些扩展方法,用于在更多地方提供 RedirectToAction 功能。这对于 OP 来说为时已晚,但希望它可以帮助某人!
public static class RedirectHelper
{
// RedirectToAction Extension Methods
public static void RedirectToAction(this HttpResponseBase response, String action, String controller, object routeValues = null, bool endResponse = false)
{
response.RedirectToRoute(CreateRoute(action, controller, routeValues));
if (endResponse) response.End();
}
public static void RedirectToAction(this HttpResponse response, String action, String controller, object routeValues = null, bool endResponse = false)
{
response.RedirectToRoute(CreateRoute(action, controller, routeValues));
if (endResponse) response.End();
}
public static void RedirectToAction(this ActionExecutingContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false)
{
if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true);
else filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues));
}
public static void RedirectToAction(this ExceptionContext filterContext, String action, String controller, object routeValues = null, bool endResponse = false)
{
if (endResponse) filterContext.HttpContext.Response.RedirectToAction(action, controller, routeValues, true);
else {
filterContext.ExceptionHandled = true;
filterContext.Result = new RedirectToRouteResult(CreateRoute(action, controller, routeValues));
}
}
// Route Value Derivation
public static RouteValueDictionary CreateRoute(String action, String controller, object routeValues = null)
{
RouteValueDictionary result = routeValues != null ?
HtmlHelper.AnonymousObjectToHtmlAttributes(routeValues) :
new RouteValueDictionary();
result["controller"] = controller;
result["action"] = action;
return result;
}
}
还有更多 ControllerContexts 不包括在内,但根据您的需要添加您自己的应该相当容易。