So I have this controller action:
public ActionResult Categories(int typecode)
{
// code removed
}
this route:
routes.MapRoute(null,
"{controller}/{action}/{typecode}",
new { controller = "Search", action = "Categories", }
);
and this link to call the route:
@Html.ActionLink("Ga", "Categories", "Search", new { typecode = 16860 }, null)
if I use this, my URL is: http://localhost:50033/Search/Categories?typecode=16860
but if I change all occurences of typecode
to id
, it works and I get this URL: http://localhost:50033/Search/Categories/16860
So with typecode my route doesn't work and with id it does. What am I doing wrong? Thanks!
EDIT:
I think I wasn't clear enough, but in my Global.asax.cs
file I have this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("TypeCode",
"Search/Categories/{typecode}",
new { controller = "Search", action = "Categories" }
);
}
So that's only ONE route, than in my SearchController
I have this Categories
action:
public ActionResult Categories(int typecode)
{
// Irrelevant code removed
}
So the parameter is exactly the same as the route parameter, then I have this link:
@Html.ActionLink("Ga", "Categories", "Search", new { typecode = 16860 }, null)
Also using exactly the route parameter, but still the generated link is: http://localhost:50033/Search/Categories?typecode=16860
so that's not what I want.
Now, when i replace all typecode occurences, like this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("TypeCode",
"Search/Categories/{id}",
new { controller = "Search", action = "Categories" }
);
}
public ActionResult Categories(int id)
{
// irrelevant code removed
}
@Html.ActionLink("Ga", "Categories", "Search", new { id = 16860 }, null)
It works! so I replaced everything, there are no more routes, I just replaced the 3 typecode
occurences with id
.
Why is this? Can anyone help me with this please? Thanks in advance!