Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, June 19, 2014 9:37 AM
Here is what I found on stackOverflow.
Question:
What's the difference in ASP.NET MVC of
RedirectToRoute
and
RedirectToAction
It's not clear what the difference is to me.
Answer:
Redirect to route looks up the route table thats defined in global.asax and redirect to action redirects you to a specified controller/action.
that's about it really
Well I have this Default route. And every one have a default value. So if I don't give any value I will be calling
Home/Index
route routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
So if I just use RedirectToRoute in the Index action of the Post controller I thought I would be calling Home/Index but instead I get the error in swedish "fel vid sidhämtning" that translates to "Error when page loads" by google translate
So why does not RedirectToRoute call Home/Index
So are these two identical ?
return RedirectToRoute("Default", new { controller="Home", action="Index" });
return RedirectToAction("Index", "Home");
public class PostController : Controller
{
private ApplicationDbContext db = new ApplicationDbContext();
// GET: /Post/
public ActionResult Index()
{
return RedirectToRoute("Default");
}
//Tony
All replies (12)
Thursday, June 19, 2014 10:33 PM ✅Answered
Hi Tojo,
Thanks for your post.
According to your description,Based on my knowledge.
RedirectToAction:
This tells MVC to redirect to specified action instead of rendering HTML. In this case, browser receives the redirect notification and make a new request for the specified action. This acts like as Response.Redirect() in Asp.Net WebForm.
Moreover, RedirectToAction construct a redirect url to a specific action/controller in your application and use the route table to generate the correct URL.
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string Name)
{
ViewBag.Message = "Hi, Welcome to Nihar's Blog";
//Like Response.Redirect() in Asp.Net WebForm
return RedirectToAction("MyIndex");
}
public ActionResult MyIndex()
{
ViewBag.Msg = ViewBag.Message;
// Assigned value : Null
return View("MyIndex");
}
RedirectToRoute:
This tells MVC to look up the specifies route into the Route table that is is defined in global.asax and then redirect to that controller/action defined in that route. This also make a new request like RedirectToAction().
In global.asax
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"MyRoute", // Route name
"Account/", // URL
new { controller = "Account", action = "Login"} // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "MyIndex", id = UrlParameter.Optional } //Parameter defaults
);
}
In Controller :
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(string Name)
{
return RedirectToRoute("MyRoute");
}
Hope this helps u.
Best Regards,
Eileen
Tuesday, June 24, 2014 12:40 AM ✅Answered
Hi Tojo,
Thanks for your reply.
Are u Using mvc3,or mvc4,mvc5? If you used mvc4 or above,You can define custom route in RouteConfig.
Actually,I think RedirectToRoute is the same as RedirectToAction.I did a sample:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"MyRoute", // Route name
"Account/", // URL
new { controller = "Account", action = "Login" } // Parameter defaults
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
Controller:
public ActionResult Index2(string Name)
{
return RedirectToRoute("MyRoute");
}
I have tested it,It worked in my side.
If you used mvc3,You can follow my above suggestion.
If you have any issue,Please let me know.
Hope this helps u.
Best Regards,
Eileen
Friday, June 20, 2014 5:47 AM
But why is it not possible for example to do
return RedirectToRoute("Default")
instead of doing
return RedirectToRoute("MyRoute")
I mean the Default route has default parameter that MVC should be able to use.
//Tony
Friday, June 20, 2014 6:11 AM
return RedirectToRoute("Default")
I sure it is considering the Default routing.
I think this is what happening, right now you executing this code from the PostController and Index action, hence your are passing the controller and action as your parameters to your default routing. This is similar to:
return RedirectToRoute("Default", new { controller = "Post", action = "Index"})
I am sure this is not what you intended.
Friday, June 20, 2014 10:23 AM
I don't understand what you mean.
I mean that if you use
return RedirectToRoute("Default");
you should be redirected to the default defined controller=Home and action=Index for this route name= Default
Friday, June 20, 2014 12:05 PM
Id also suggest adding a route that will redirect the Default.aspx page. Depending how IIS is setup with the default document, it may hit this first, which may also be a part of your problem. I had a very similiar issue to yours where i could not figure out why the page would not redirect.
routes.MapRoute(
"Start",
"Default.aspx",
new { controller = "Home", action = "Index", id = "" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Friday, June 20, 2014 7:44 PM
But both controller and action have devault values and Id is optional
//Tony
Friday, June 20, 2014 9:02 PM
you should be redirected to the default defined controller=Home and action=Index for this route name= Default
In the routing the default values of contoller & action will be used only if you don't pass them. RedirectToRoute is a method in the controller, hence you are implicitly passing the controller and action name.
The below two are exactly same for you
return RedirectToRoute("Default", new { controller = "Post", action = "Index"})
return RedirectToRoute("Default");
To try this put a break point in the PostController.Index and see that this redirect will cause infinite loop.
Saturday, June 21, 2014 5:15 AM
Can you specify a good reason when it would be good to use this RedirectToRoute because
when it now works about the same as RedirecToAction I can't see when it would be good to use it.
//Tony
Sunday, June 22, 2014 4:14 AM
Some explantion below.
http://just4mvc.blogspot.com/2013/03/return-view-vs-return-redirecttoaction.html
Monday, June 23, 2014 7:56 AM
Can you specify a good reason when it would be good to use this RedirectToRoute
You should use RedirectToRoute when you have multiple routes and you know that you want to bypass the MVC routing with a routing you know. Otherwise you should use RedirectToAction.
In your case you have only one routing, hence you don't need RedirectToRoute.
Tuesday, June 24, 2014 1:34 AM
There is no difference in RedirectToRoute and RedirectToAction in terms of end results because both methods redirects the current request to another action/controller.
Can you specify a good reason when it would be good to use this RedirectToRoute because
when it now works about the same as RedirecToAction I can't see when it would be good to use it.
Both RedirectToRoute and RedirectToAction cause additional roundtrip and because of this newly created url is displayed in browser. Routes are normally defined to create urls that are user friendly, looks good and are good for SEO. So if shape of end ur is very important and it is other than just controller/action/param then use RedirectToRoute, otherwise use RedirectToAction.