0

How do I change the following code to produce the proper URL?

Note: The ActionLink is being produced from a different controller than DomainsController.

@Html.ActionLink("Domains", "Index", "Domains", new { id = item.Name }, null)

The goal is to see the following URL.

/Domains/ItemName

What I'm getting is:

/Domains/Index/ItemName

Any help is greatly appreciated.

alockrem
  • 767
  • 3
  • 9
  • 23

2 Answers2

0

Strange routing :)

Here goes quick and dirty solution:

@{string link= "Domains/" + item.Name;}
@Html.ActionLink("Domains", "", link, null, null)
MiBu
  • 869
  • 8
  • 17
  • What is your suggestion for the proper URL? /ItemName/Domains? That makes more sense, but I'm not sure how to implement that either. The goal is to have a user-friendly URL that displays the domains associated with a specific item. – alockrem Aug 25 '12 at 20:12
  • It depends on your structure, security requirements... I would look into options with routing to find an elegant solution, here is the link with MVC resources, and has section with routing articles: http://msdn.microsoft.com/en-us/library/gg416514(VS.98).aspx – MiBu Aug 27 '12 at 06:34
0

Your requirement is little impractical. You certainly can achieve that with something like

routes.MapRoute(null,
                "Domains/{id}",
                new { controller = "Domains", action = "Index" }
);

But then, you would have to create explicit routing rules for all of the other DomainsController action methods, otherwise you wouldn't be able to use simply e.g. /Domains/OtherAction anymore - it would be caught by the aforementioned rule and interpreted as a call to the Index method with id == "OtherAction"

twoflower
  • 6,788
  • 2
  • 33
  • 44