既然我已经决定让同时用贝塔停留rc现在走了,我不知道一个强类型RedirectToAction是否已经添加的方式。有没有人尝试过了,是有一个强类型RedirectToAction(也许ActionLink)在RC?

有帮助吗?

解决方案

没有,它不会。

protected RedirectToRouteResult RedirectToAction<T>(Expression<Action<T>> action, RouteValueDictionary values) where T : Controller
{
    var body = action.Body as MethodCallExpression;

    if (body == null)
    {
        throw new ArgumentException("Expression must be a method call.");
    }

    if (body.Object != action.Parameters[0])
    {
        throw new ArgumentException("Method call must target lambda argument.");
    }

    string actionName = body.Method.Name;

    var attributes = body.Method.GetCustomAttributes(typeof(ActionNameAttribute), false);
    if (attributes.Length > 0)
    {
        var actionNameAttr = (ActionNameAttribute)attributes[0];
        actionName = actionNameAttr.Name;
    }

    string controllerName = typeof(T).Name;

    if (controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase))
    {
        controllerName = controllerName.Remove(controllerName.Length - 10, 10);
    }

    RouteValueDictionary defaults = LinkBuilder.BuildParameterValuesFromExpression(body) ?? new RouteValueDictionary();

    values = values ?? new RouteValueDictionary();
    values.Add("controller", controllerName);
    values.Add("action", actionName);

    if (defaults != null)
    {
        foreach (var pair in defaults.Where(p => p.Value != null))
        {
            values.Add(pair.Key, pair.Value);
        }
    }

    return new RedirectToRouteResult(values);
}

这应该工作。

其他提示

此也被包括在 MVC的Contrib 的作为控制器上的延伸法,用大量的沿着其他强类型的好东西的ModelState处理,测试等,这是非常值得考虑的额外依赖它所提供。

可以使用return RedirectToAction(nameof(Index));

如果你不想全部MvcContrib库,您可以通过使用 MvcNavigationHelpers NuGet包。

scroll top