MVC ActionLink generates different types of links ... why?

I am new to MVC land and have an application I'm working on. I have two different links with two routes in my global that are pretty similar to each other.

route 1

routes.MapRoute("Category", "Movies/{category}/{subcategory}", new { controller = "Catalog", action = "Index", category = "", subcategory = "" }); 

route 2

 routes.MapRoute("Movie", "Movie/{movie}", new { controller = "Movie", action = "Index", movie = "" }); 

When I call actionlink for the first route, it creates it, since I think it should:

.../Movies/Category/SubCategory

however, when I create my second link, it fills it as follows:

.../Movie?movieId=ff569575-08ec-4049-93e2-901e7b0cb96a

I used the string instead of guid before and it still did the same ie

.../Movie?movieName=Snatch

my actionlinks are configured as follows

 <%= Html.ActionLink(parent.Name, "Index", "Catalog", new { category = parent.Name, subCategory = "" }, null)%> <%= Html.ActionLink(movie.Name, "Index", "Movie", new { movieId = movie.MovieId }, null)%> 

My application is still working, but I thought this behavior was strange. any help would be great.

Thanks!

+4
source share
2 answers
 routes.MapRoute("Movie", "Movie/{movieId}", new { controller = "Movie", action = "Index", movie = "" }); 

If the route text does not match the name of the property that you are sending to the mvc link?

+5
source

The problem is that when calling ActionLink, the routing system cannot determine which of the two routes to use, so it chooses the first. The solution is to use RouteLink instead of ActionLink. RouteLink allows you to specify the name of the route used to generate the URI. Then there is no ambiguity as to which route to use. I think ActionLink is deprecated. I cannot think of any reason to use it instead of RouteLink.

However, you may have a problem when the user submits links. In this case, use route restrictions to ensure that you select the correct route.

Andrew correctly (voted) that the tokens that you use in ActionLink / RouteLink must match the route itself.

+4
source

All Articles