4

I have an MVC Action Link:

@Html.ActionLink("Update Information", "Index", "Performance", 
new { performanceid = item.PerformanceId }, null)

This action link's href looks like this: /Performance/Index?performanceid=100

In my RouteConfig.cs I have the following routes in the following order:

routes.MapRoute(
      "ShowPerformanceOptions",
      "Performance/{performanceid}/Index",
      new { controller = "Peformance", action = "Index" }
);

routes.MapRoute(
      "Default",
      "{controller}/{action}/{id}",
      new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

I do not want a querystring added to the end of the URL, I would instead like the URL to look like this: /Performance/360/Index

I have been through a variety of different options including adding the route parameters and optional url parameters and changing the way I write my ActionLink. Nothing seems to work.

Any ideas anyone?

  • You're having a typo in the default route params defintion, it's `Peformance` instead of `Performance`. – haim770 Jul 28 '15 at 06:42
  • This also works! Good spot, I thought there must have been something wrong for it not to recognise any of my routes that I had specified! Thanks – Christian Coan Jul 28 '15 at 06:45

2 Answers2

2

To generate URL based on route name, use Html.RouteLink() method

@Html.RouteLink("Update Information", "ShowPerformanceOptions", new { performanceid = item.PerformanceId })

A good read What's the difference between RouteLink and ActionLink in ASP.NET MVC?

Community
  • 1
  • 1
Satpal
  • 132,252
  • 13
  • 159
  • 168
2

As @Satpal pointed out, the ActionLink wasn't working because of the typo in the route itself:

routes.MapRoute(
      "ShowPerformanceOptions",
      "Performance/{performanceid}/Index",
      new { controller = "**Peformance**", action = "Index" }
);

routes.MapRoute(
      "ShowPerformanceOptions",
      "Performance/{performanceid}/Index",
      new { controller = "**Performance**", action = "Index" }
);