In my controller of webpage 1, I want to redirect to Webpage 2, passing 2 variables.
I tried using RedirectToRoute, but cannot get it to work; wrong URL is displayed. I then switched to using RedirectToAction.
my code:
Routing
routes.MapRoute(
"CreateAdditionalPreviousNames", // Route name
"Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters
new { controller = "UsersAdditionalPreviousNames", action = "Index", userId = UrlParameter.Optional, applicantId = UrlParameter.Optional } // Parameter defaults
);
RedirectToAction (which works)
return RedirectToAction("Index", "UsersAdditionalPreviousNames", new { userId = user.Id, applicantId = applicant.Id });
RedirectToRoute (doesn't work)
return RedirectToRoute("CreateAdditionalPreviousNames", new { userId = user.Id, applicantId = applicant.Id });
Oh, and one other thing, can you make parameters required, rather than optional....if so, how?
RedirectToAction will return a http 302 response to the browser and then browser will make GET request to specified action. Show activity on this post. Ideally I would use RedirectToRoute for Action Links/Images and RedirectToAction in Controller's Action to redirect to another Controller's Action .
RedirectToRoute(Object) Redirects to the specified route using the specified route values. RedirectToRoute(String) Redirects to the specified route using the route name.
RedirectToAction(String, String, Object)Redirects to the specified action using the action name, controller name, and route dictionary.
The RedirectToAction() MethodThis method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action. This acts just like as Response. Redirect() in ASP.NET WebForm.
Redirect to route looks up the route table thats defined in global.asax and redirect to action redirects you to a specified controller/action. Show activity on this post. RedirectToRoute means it redirects to a specific URL which is defined in routing API. (Global.asax)
return RedirectToAction () To redirect to a different action which can be in the same or different controller. It tells ASP.NET MVC to respond with a browser to a different action instead of rendering HTML as View () method does. Browser receives this notification to redirect and makes a new request for the new action.
The RedirectToAction() Method. This method is used to redirect to specified action instead of rendering the HTML. In this case, the browser receives the redirect notification and make a new request for the specified action.
It is rendered to the page by URL. If we give the wrong URL, it will show 404-page errors. The RedirectToRouteResult is used whenever we need to go from one action method to another action method within the same or different controller in ASP.NET MVC Application.
Omit parameter defaults to make parameters required:
routes.MapRoute( "CreateAdditionalPreviousNames", // Route name "Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters new { controller = "UsersAdditionalPreviousNames", action = "Index" } );
For route redirect, try this:
return RedirectToRoute(new { controller = "UsersAdditionalPreviousNames", action = "Index", userId = user.Id, applicantId = applicant.Id });
Another habit I picked up from Steve Sanderson is not naming your routes. Each route can have a null name, which makes you specify all parameters explicitly:
routes.MapRoute( null, // Route name "Users/{controller}/{action}/{userId}/{applicantId}", // URL with parameters new { controller = "UsersAdditionalPreviousNames", action = "Index" } );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With