如何在Controller扩展中进行RedirectToAction

时间:2012-06-12 07:13:46

标签: asp.net-mvc model-view-controller

我正在尝试进行扩展

public static ActionResult RedirectToIndexAction(this Controller controller)
        {
            return RedirectToAction("Index");
        }

RedirectToAction("Index")使RedirectToIndexAction();

1 个答案:

答案 0 :(得分:1)

关于以下内容:

public static RedirectToRouteResult RedirectToAction<TController>(this Controller controller, Expression<Action<TController>> action, object routeValues) where TController : Controller
    {
        RouteValueDictionary routeValuesFromExpression = ExpressionHelper.GetRouteValuesFromExpression(action); 

        var routeValueDictionary = new RouteValueDictionary(routeValues); 
        foreach (KeyValuePair<string, object> keyValuePair in routeValueDictionary)
        {
            routeValuesFromExpression.Add(keyValuePair.Key, keyValuePair.Value);
        }
        return new RedirectToRouteResult(routeValuesFromExpression);
    }


     private RedirectToRouteResult RedirectToIndex()
    {
        return this.RedirectToAction<MyController>(c => c.Index(), new { Area = "" });
    }