I have two routes, when I use the Default route it shows the url with Question mark in the parameter. I’m getting Editar/id?3 — instead Editar/id/3. The other route is working fine, could you give me some light. Thanks.
Global.asax
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"GaleriaCustom", // Route name
"{controller}/{action}/{categoria}/{subCategoria}", // URL with parameters
new { controller = "Galeria", action = "Index", categoria = "Noivinhos", subCategoria = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
}
}
Index.aspx — wrong url Editar/id?3 — instead Editar/id/3
<a href="<%: Url.Action("Editar", new{ id = item.galeria_id }) %>"><img src="<%: Url.Content("~/Content/Img/Design/editar.png") %>" alt="Editar" title="Editar"/></a>
<a href="<%: Url.Action("Detalhe", new { id=item.galeria_id }) %>"><img src="<%: Url.Content("~/Content/Img/Design/detalhe.png") %>" alt="Detalhe" title="Detalhe"/></a>
<a href="<%: Url.Action("Excluir", new { id=item.galeria_id }) %>"><img src="<%: Url.Content("~/Content/Img/Design/delete.png") %>" alt="Excluir" title="Excluir"/></a>
Your first route is more greedy than the second and unless you use some constraints it will match all urls. Another possibility is to use the
RouteUrlhelper and specify the name of the route: