質問

私は今のベータ版で滞在しながら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);
}

これは動作するはずです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top