ActionLink in the form of Routing by parameters instead of QueryString

2

I'm trying to make it use a ActionLink with multiple parameters to generate a link to me in the form

{Controller}/{Action}/{idCompania}/{idLocal}/{fecha}

But it always generates a querystring :

{Controller}/{Action}?idCompania=0&idLocal=21&fecha=23-12-2015

In the global.asax I have:

routes.MapRoute(
            "UltimosInformesDetail",
            "UltimosInformes/Details/{idCompania}/{idLocal}/{fecha}",
            new { controller = "UltimosInformes", 
                  action = "Details", 
                  idCompania="", 
                  idLocal="", 
                  fecha="" },
            null
)

My ActionLink in the view:

<td>
    @Html.ActionLink("Detalle", 
                     "Details",
                     "UltimosInformes", 
                     new { idCompania = item.Compania, 
                           idLocal    = item.Local.Codigo, 
                           fecha      = item.Fecha.ToString("dd-MM-yyyy")}, null)
</td>

My controller :

    public ActionResult Details(short idCompania, short idLocal,string fecha)
    {
        var s = RootContext.Instance.Resolve<ProblemaService>();

        return View(s.ObtenerAuditoresVisita(idCompania, idLocal, DateTime.Parse(fecha)));
    }
  

I researched in SO in English see here and here But I could not solve it

I can not make it generate the "routed" link, it always generates a querystring

    
asked by Alan 23.12.2015 в 15:36
source

1 answer

3

I've tried your route and the syntax is fine.

The only thing that possibly generates the error is that the path "UltimosInformesDetail" that you have created you have registered after the default route

You should always go the more specific routes first , then the more general routes and at the end (if you still need it) the default route.

routes.MapRoute(
    "UltimosInformesDetail",
    "UltimosInformes/Details/{idCompania}/{idLocal}/{fecha}",
    new
    {
        controller = "UltimosInformes",
        action = "Details",
        idCompania = "",
        idLocal = "",
        fecha = ""
    },
    null
);

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