R – Problems with a custom route in ASP.NET-MVC

asp.net-mvc-routing

I am having problems with Html.ActionLink when I have a route that takes one parameter.
I have the following routers in global.asx:

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );
        routes.MapRoute(
            "materias",
            "{controller}/{action}/{id},{titulo_materia}.html",
            new { controller = "materias", action = "Index", id = "", titulo_materia = "" }  
        );

When I use ActionLink passing two parameters, everything works ok.
But when I try to create a link using the first route I end up with something like:

http://meusite.com/controller-name/[parameter],.html

EDIT:

Here is the action link that i`m having problems:

<span class="editar"><%=Html.ActionLink("Editar", "Edit", "Users", new { id = this.Model.login }, null)%></span>

This link is on another page that is used to manage user data.

Best Answer

First you should put your most generic route at the bottom.

Then, how about doing something like :

    routes.MapRoute(
        "materias",
        "{materias}/{action}/{id},{titulo_materia}.html",
        new { controller = "materias", action = "Index", id = "", titulo_materia = "" }  
    );

    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );

This way , the materias route only works for the materias controller. (not tested)

EDIT: htmm .. try using martin's example with a small addition :

Html.RouteLink("Link Title", new { controller = "Controller", Action= "Action",id = this.Model.login });
Related Topic