Bigsby
Bigsby

Reputation: 952

Html.ActionLink Not Behaving as Expected

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.

Upvotes: 1

Views: 118

Answers (2)

Ibrahim Khan
Ibrahim Khan

Reputation: 20740

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"
);

Upvotes: 1

Julius Depulla
Julius Depulla

Reputation: 1633

//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();
 }
}

Upvotes: 0

Related Questions