with asp.net-mvc mvccontrib url-redirection

asp.net-mvc - with - redirecttoaction post c#



¿.NET MVC tiene un RedirectToAction fuertemente tipado? (4)

No, no es así.

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); }

Eso debería funcionar.

Como he decidido dejar ir a RC durante mi estancia con Beta por ahora, no tengo forma de saber si se ha agregado un RedirectToAction fuertemente tipado. ¿Alguien lo ha probado y hay un RedirectToAction fuertemente tipado (y tal vez ActionLink ) en RC?


Esto también se incluye en MVC Contrib como un método de extensión en su controlador, junto con muchas otras cosas bien tipadas para el manejo, prueba, etc. de ModelState. Vale la pena asumir la dependencia adicional de lo que ofrece.


Puede usar return RedirectToAction(nameof(Index));


Si no desea la biblioteca completa de MvcContrib, puede obtener solo esta característica mediante el uso del paquete MvcNavigationHelpers NuGet.