I am trying to implement “returnUrl” functionality in my aspnetcore app but I am struggelling to find a way to dynamically pass the parameters to an action.
The process flow goes like this:
- The user has bookmarked URL
https://localhost:44358/Secure/VisualizationFiles?viz=vizFilePlays - the website checks if they are logged in and if not redirects them to Home controller’s
Loginaction to log in (passing the returnUrl) - after logging in the user is redirected to the originally requested URL above including any URL parameters
Right now the Secure controller methods use ActionFilterAttribute which intercept the incoming HTTP request and make sure the controller; the action and parameters get passed as returnUrl (or reRoute in that case)
public class NimsAuthenticationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var url = filterContext.RouteData.Values["Controller"] + "/" + filterContext.RouteData.Values["Action"] + "?" + QueryString(filterContext.ActionArguments);
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "controller", "Home" }, { "action", "Index" }, { "reRoute", url } });
base.OnActionExecuting(filterContext);
}
private string QueryString(IDictionary<string, object> dict)
{
var list = new List<string>();
foreach (var item in dict)
{
list.Add(item.Key + "=" + item.Value);
}
return string.Join("&", list);
}
}
After the user gets redirected (from the secure controller) to the Home controller, we get the full returnUrl (i.e. reRoute URL) being passed
As you can see, right now I am doing some error-prone string manulupation to extract the Controller; Action and url params from the reRoute string. I would like to know:
- Is there a way I can pass the whole
reRouteurl to theRedirect? - If the above is not possible, is there an alternative way to handle the redirect so that it is dynamic (i.e. the url parameters will be different as well as the Controller and Action)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can use the Redirect Method instead of RedirectToAction.
So in you case it would look like this:
return Redirect("/" + reRoute);
The / is there to go to the root url, otherwise the request would route relative to the current controller.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

