How do I create an ActionLink to the Index action of a different controller?
-
28-06-2021 - |
Question
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.
La solution
Strange routing :)
Here goes quick and dirty solution:
@{string link= "Domains/" + item.Name;}
@Html.ActionLink("Domains", "", link, null, null)
Autres conseils
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"
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow