1

I have stumbled upon a curious behavior in ASP.NET MVC Routing.

In my RouteConfig file, when I map a route like this (the default route):

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional
            }
        );

Using:

@Html.ActionLink("Index", "Home")

I get a nice, clean and short URL, like: http://mysite/

But if I add another optional parameter after id, like this:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}/{name}",
            defaults: new {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional,
                name = UrlParameter.Optional
            }
        );

The same ActionLink outputs this URL: http://mysite/home/index every time. I verified the same behavior using RedirectToAction.

My questions are: Is there a way to work around this and get the shorter URL in the later case? Why ASP.NET MVC Routing engine behaves differently in these cases?

EDIT

I managed to work around this issue following the instructions posted by Dave A. I added a custom route before the "Default" route that matches my custom URL pattern.

routes.MapRoute(
    name: "Custom",
    url: "{controller}/{action}/{id}/{name}",
    defaults: new {
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional
    }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new {
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional
    }
);
5
  • where are you rendering these routes? Are you always venturing from /Home/Index? Commented Mar 13, 2013 at 3:23
  • I tried from different actions/controllers and always got the same results in both cases. Commented Mar 13, 2013 at 3:30
  • 2
    There are indeed "bugs" or inefficiencies in the way the MVC framework handles routes. In this case, especially, it is trying to make best use of all params but getting confused. Read this post by hacked that shows more "glitches" in the system haacked.com/archive/2011/02/20/… Commented Mar 13, 2013 at 3:41
  • I believe System.Web.Routing mechanism is same in .NET 4 and .NET 4.5. The issue is related with routing and MVC 3/4. You can get the detail and a temporary fix at, weblogs.asp.net/imranbaloch/archive/2010/12/26/…. Commented Mar 14, 2013 at 5:41
  • @DaveA Thanks! You should post this as answer. Commented Mar 14, 2013 at 14:56

1 Answer 1

3

There are indeed "bugs" or inefficiencies in the way the MVC framework handles routes. In this case, especially, it is trying to make best use of all params but getting confused.

Read this post by hacked that shows more "glitches" in the system http://weblogs.asp.net/imranbaloch/archive/2010/12/26/routing-issue-in-asp-net-mvc-3-rc-2.aspx

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.