Matin Habibi
Matin Habibi

Reputation: 700

MVC 3 - ActionLink

I am using RegisterRoutes method in Global file to route user url to actual url.

routes.MapRoute("Blog", 
                "blog/dp/{id}", 
                 new { controller ="Blog", action = "Details" });

As you might have guessed, Blog is the controller and Details is its action.

So the problem is that the following code does not generate my desire URL which has dp word in its url. By the way, I don't want to change my Action Name.

@Html.ActionLink( "headline", "Details", "Blog", new { id="1200" }, null )

Thanks in advance ;)

Upvotes: 3

Views: 121

Answers (2)

saintedlama
saintedlama

Reputation: 6898

You could use a RouteLink instead of using an ActionLink. In a RouteLink you can explicitly pass your route name:

@Html.RouteLink("headline", "Blog", new { controller = "Blog", action = "Details" })

More about RouteLink method can be found in the MSDN. Reordering routes can do the job but that's a rather fragile mechanism.

Upvotes: 3

John Allers
John Allers

Reputation: 3122

The ActionLink may be using a different route from what you are providing in the question. Do you have any routes declared before this one?

The routes are matched in the order that you provide them. So, for instance, if you had something like:

routes.MapRoute("Blog", 
                "blog/dp",       // {id} is not provided in this one
                 new { controller ="Blog", action = "Details" });

declared before this route:

routes.MapRoute("Blog", 
                "blog/dp/{id}", 
                 new { controller ="Blog", action = "Details" });

The first route would be matched first and you would see /blog/dp/?id=1200 instead of /blog/dp/1200.

Upvotes: 1

Related Questions