Asp.net mvc: make RedirectToAction (string, object) in RedirectToAction <Controller> (x => x.Detail (id))

Does anyone know how to make a method (I'll put it in an extension class) that will do the same as mvc RedirectToAction, only using expressions (no magic lines).

So, instead of writing something like this:

 RedirectToAction("Detail", 
    new RouteValueDictionary { {"messageId", messageId}});

I would do the following:

this.RedirectToAction(x => x.Detail(messageId));

I tried and did something like this, but I don't know how to add parameters:

 public static RedirectToRouteResult RedirectToAction<T>(this T controller,
     Expression<Action<T>> expression)
 {
     return RedirectToAction(
        (expression.Body as MethodCallExpression).Method.Name
     );
 }
+3
source share
2 answers
+1

Source: https://habr.com/ru/post/1729254/


All Articles