.NET MVC是否具有强类型RedirectToAction?

Use*_*ser 19 asp.net-mvc mvccontrib url-redirection

由于我现在决定让RC继续使用Beta,我无法知道是否RedirectToAction添加了强类型.是否有人尝试过它并且在RC中是否存在强类型RedirectToAction(也许ActionLink)?

Dar*_*ngo 25

这也包含在MVC Contrib中作为控制器的扩展方法,以及用于ModelState处理,测试等的许多其他强类型的好东西.非常值得对它提供的内容进行额外的依赖.


Cha*_*ran 17

不,它没有.

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);
}
Run Code Online (Sandbox Code Playgroud)

这应该工作.

  • 整个MVC框架应该像这样强类型,我希望它们会抛弃魔术字符串,它就是"DataSet".Thanx代码乍得. (4认同)
  • 什么是`LinkBuilder`? (3认同)

Cri*_*jak 5

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