1

I'm trying to get a pretty URL from Html.ActionLink but am getting the querystring version instead. I have a route defined that works perfectly when I enter what the values should be into the navigation bar in my browser and end up getting the views and hitting the controller actions I think I would.

My route definition:

routes.MapPageRoute(
    "WebFormDefault",
    "",
    "~/Login.aspx"
    );

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

My helper call markup:

<ul>
    <li>@Html.ActionLink("Foo Link", "FooAction", "Foo")</li>
</ul>

Actual result:

<a href="/?action=FooAction&amp;controller=Foo">Foo Link</a>

My expectation:

<a href="/Foo/FooAction">Foo Link</a>

I did try starting a new project outside of the current one, which is an existing application, and saw the expected result. I've added MVC manually to the project as it's a webforms application originally. This is a hybridization of sorts and am unsure if that's causing an issue. I used nuget to bring in the MVC assemblies and dependencies and setup the configs myself based on the output in VS from the "New Project..." template. I see no difference between the two in respect to the configs outside of my inclusion of our root namespace so I can use my preexisting types. However, I don't know if there's something else I need to do. It's confusing, though, because MVC has behaved in the hybrid environment otherwise exactly as I would expect it to.

Bigsby
  • 952
  • 6
  • 20

2 Answers2

1

Add mvc route before webform route. Hope this will work.

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

routes.MapPageRoute(
    "WebFormDefault",
    "",
    "~/Login.aspx"
);
Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
  • I don't know why the order matters, but it worked. I would think the MapRoute vs. MapPageRoute and a call to ActionLink defining controllers and actions would see the appropriate route but apparently not. – Bigsby Nov 25 '15 at 13:20
  • In case of multiple route first route that is fully satisfied will work. – Ibrahim Khan Nov 25 '15 at 13:33
  • I get that. I would have figured the runtime would have divided "obviously MVC routes" and "obviously webforms routes." It just seems like one of those things that should be a feature to me. – Bigsby Nov 25 '15 at 13:37
0
//To get this : <a href="/Foo/FooActionMethodName">Foo Link Text</a>

//Your action link should be
<ul>
    <li>@Html.ActionLink("Foo Link Text", "FooActionMethodName", "FooController")</li>
</ul>

//Your controller should be
public class FooController
{
 publics ActionResult FooActionMethodName()
 {

  return View();
 }
}
Julius Depulla
  • 1,493
  • 1
  • 12
  • 27