Search code examples
asp.net-mvcrazor

ASP.NET MVC routes - why the difference?


I'm new to ASP.NET MVC and am playing with the albumstore tutorial. In my index view I have a loop like this.

@foreach (var genre in Model) 
{
    <li>@Html.ActionLink(genre.Name, "Browse", new { genre = genre.Name })</li>       
}

which results in the url

http://localhost:59443/store/Browse?genre=Dicso

in my browse view I have similar code

@foreach (var album in Model.Albums) 
{
    <li>@Html.ActionLink(album.Title, "Details", new { id = album.AlbumId })</li>
}

however this produces a url with the structure

http://localhost:59443/store/Details/2

Why are the structures different using the same code?


Solution

  • Id is a special case because of the routing setup in global.asax.

    In global.asax you will find something akin to this.

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    
    }