Reputation: 4440
I have the following method call in my controller method:
RedirectToRouteResult redirect = GetRedirect();
It provides the redirect where the user has to be sent to in a HttpPost action. I need this because it's not unequivocal where the user has to be sent to 'cause there are different possibilities to go to the get method which calls the post after sending a form, e. g.:
Page A -> Create A Get -> Create A Post ---> send back to Page A
Page B -> Create A Get -> Create A Post ---> send back to Page B
return Json(new { redirectTo = UrlHelper.Action("Method", "Controller") });
I'd now like to use my redirect instead of the hard coded strings. So I have to convert the RedirectToRouteResult object or use an alternative to UrlHelper.Action but I don't know how to.
Upvotes: 1
Views: 1366
Reputation: 17108
Okay so if I understood you correctly then you can make the url generation dynamic by doing this:
var url = Url.Action(
redirect.RouteValues["controller"],
redirect.RouteValues["action"]);
return Json(new { redirectTo = url });
or a better approach, especially if the ActionResult have routevalues:
return Json(new { redirectTo = Url.RouteUrl(redirect.RouteValues) });
Upvotes: 1