Note that there are some explanatory texts on larger screens.

plurals
  1. POApparent inconsistency in ASP.NET MVC routing behaviour
    text
    copied!<p>Given the controller below</p> <pre><code>public class MyController : Controller { public ActionResult Index() { return View(); } public ActionResult Test() { return RedirectToRoute(new { action = "Index" }); } } </code></pre> <p>And this routing</p> <pre><code>public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Route1", "Route1", new {controller = "Account", action = "Index"}); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } </code></pre> <p>When I browse to My/Test then MyController's Test Action is hit as expected. Then the <code>RedirectToRoute</code> result uses Route1 as I have specified the Action as "Index". And as I didn't specify a Controller it defaults to "Account". This hits the AccountController's Index Action as expected</p> <p>If I change the routing to the code below then it works differently and not as I expect. When I browse to My/Test then this redirects to MyController's Index Action and not HomeController's Index Action.</p> <pre><code>routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional }); </code></pre> <p>I know when <code>RedirectToAction</code> is used then MVC will infer the current Controller so this would redirect to MyController's Index Action but code comments in the MVC source says <code>RedirectToRoute</code> doesn't infer the current Controller and I have hopefully proved this with the 1st example.</p> <p>Anybody have any idea why this is happening?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload