0

For the most part, my links (see an example below) work fine, but occasionally something odd will happen in that the actual URL that the link goes to makes no sense at all.

Working link example
This link works perfectly fine. The class contains a background image so that the link can be seen on the page.

@Html.ActionLink(" ", "Edit", new { id = user.PKey }, new { @class = "edit-button" })

This link navigates to ~/Users/1 assuming an id value of 1 is passed to it.

Broken link
This link doesn't work at all:

@Html.ActionLink(User.Identity.Name, "Manage", "Account", new { name = User.Identity.Name })

This link appears as part of a welcome message. The link text is my username, so I figured it would navigate to ~/Account/Manage/ortund but instead it navigates to ~/Home/Manage?Length=7

As yet the Manage ActionResult in the Account Controller does nothing, but when I get to coding it in, it will just return a view that allows the logged-in user to update their profile and see their activity.

As far as I can see there's no difference in these 2 links aside from the class that's being set on one of them. Can someone help me to understand why the 2nd link doesn't work?

Thanks in advance!

Ortund
  • 8,095
  • 18
  • 71
  • 139
  • Possible duplicate of [MVC3, Razor. Why actionlink autogenerating ?length=7 in URL?](http://stackoverflow.com/questions/4357856/mvc3-razor-why-actionlink-autogenerating-length-7-in-url). – Zabavsky Sep 11 '14 at 12:27
  • Your question should not be answered but closed as a duplicate. Please use search before asking a new question. – Zabavsky Sep 11 '14 at 12:34
  • @Zabavsky its not a duplicate. The answer on the question you linked didn't result in a working link. – Ortund Sep 11 '14 at 12:45
  • 1
    The overload you are trying to use doens't exist. Try `@Html.ActionLink(User.Identity.Name, "Manage", "Account", new { name = User.Identity.Name }, null)` – Carl Sep 11 '14 at 12:51
  • Should it be `@Html.ActionLink(User.Identity.Name, "Manage", "Account", new { name = User.Identity.Name }, null)`? –  Sep 11 '14 at 12:52
  • You are using the overload which adds attributes to the generated element - Carls comment is the answer (it's taking the length of the `name` string) – Charleh Sep 11 '14 at 12:52
  • @Carl I tried that one. Now I get `~/Account/Manage/name=ortund` but what I want is `~/Account/Manage/ortund` – Ortund Sep 11 '14 at 13:18
  • you need to add a new route for that: `routes.MapRoute("AccManage", "Account/Manage/{name}", New With {.controller = "Account", .action = "Manage"});` so that MVC knows it is the value of the 3rd part of the url – Carl Sep 11 '14 at 13:22
  • @Carl thanks. Please create an answer so I can accept. Also, no need for "with { .controller..." simply "new { controller = "Account"...." works here – Ortund Sep 11 '14 at 13:34
  • @Ortund, sorry mixed VB with C# syntax! Answer posted, cheers. – Carl Sep 11 '14 at 13:45

1 Answers1

1

To answer the original question, the wrong overload was being used. The correct overload in this instance is:

LinkText, ActionResult, Controller, RouteValues (object), HTMLAttributes(object) - e.g.:

@Html.ActionLink(User.Identity.Name, "Manage", "Account", new { name = User.Identity.Name }, null)

The second part of the Q - to make the variable "name" part of the URL instead of a querystring value - e.g. from:

~/Account/Manage/name=ortund

to

~/Account/Manage/ortund

You need to add a route to your route config so that MVC knows that the third part of your url should contain the value of "name" - this is done with the following route:

routes.MapRoute("AccManage", "Account/Manage/{name}", new {.controller = "Account", .action = "Manage"});
Carl
  • 2,285
  • 1
  • 16
  • 31