redirecting to another action

avnish
avnish
Participant
242 Points
11 Posts

What is the difference between RedirectToRoute and RedirectToAction methods.

I mean both methods are made to hit a perticular action,then hw to decide which one to use and when

Views: 9095
Total Answered: 1
Total Marked As Answer: 1
Posted On: 18-May-2015 09:00

Share:   fb twitter linkedin
Answers
Rahul Maurya
Rahul M...
Teacher
4822 Points
23 Posts
         

Hi Avnish,

There are three method in MVC to redirect to other action as:

  • RedirectToAction()
  • Redirect()
  • RedirectToRoute()

All method make new request.

RedirectToAction():

Use it if you know ActionName within a Controller as:

RedirectToAction("ActionName", new { });

Use it if you know ActionName and ControllerName as:

RedirectToAction("ActionName", "ControllerName", new { });

Redirect():

Use it if you do not know actionname or ControllerName, just you know only path as:

Redirect("Home/MyIndex");

 RedirectToRoute():

Use it if you know specific routename from route config or global.asax as:

 public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
//Tutorial Routing Starts
routes.MapRoute( 
"MyRoute", // Route name 
"Login", // URL with parameters 
new { controller = "Account", action = "Login" } // Parameter defaults
);
}
 
 
RedirectToRoute("MyRoute", new { });

 

Posted On: 18-May-2015 20:26
 Log In to Chat