문제

지금은 베타 버타를 유지하면서 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 너겟 패키지.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top