I just updated a WebForms/ASP.NET MVC 3 hybrid application to use mvc3 and the razor view engine. Everything seemed to be working fine however, now when I use Html.ActionLink() my links do not get resolved correctly or something?
For Example:
@Html.ActionLink("Create New", "Create")
generates an anchor tag like this:
<a href="/?action=Create&controller=Network">Create New</a>
instead of what I would expect:
<a href="/Network/Create">Create New</a>
Here is the controller action method:
public class NetworkController : Controller {
public ActionResult Create() {
return View(new Network());
}
}
Any suggestions would be great. Thank you.
As others have said, there’s definitely something up with your route config.
The routing engine does two things; it resolves a URL to a set of routevalues (which is what’s happening when you type /Network/Create in your browser and it goes to the correction action) and it resolves a set of routevalues to a URL (which is what happens when you do URL generation like in an actionlink). The routes are tested in order, and the first one that declares a match wins, which means you don’t always have the same result both directions if you haven’t set them up right.
Recommend doing route testing w/ the MVCContrib route helpers, which may help you narrow down your problem. Also, it’s generally a bad practice (or at least unreliable) to rely on the “ambient” route values, as @Chad Moran suggested, if you choose oneof the more explicit overloads of ActionLink you may hve better results as well.