I’m trying to pass a list of URL’s with Id attributes from a controller to a view.
I can pass a <a href=...> link back but I don’t think writing a ‘localhost’ absolute path is a clean way of approaching this. I cant pass an ActionLink back as it returns the full string. Is ther a simple solution to this problem? Thanks in advance.
I’m trying to pass a list of URL’s with Id attributes from a controller
Share
Using this overload of the
UrlHelper.Action()method andRequestobject you can get a complete URL including the route parameters such as IDs and the actual hostname of the application.UrlHelperis available in the controller via itsUrlproperty.You can then pass such URL into your view.
It is also possible to use
UrlHelperdirectly inside your view to create URLs for controller actions. Depends if you really need to create them inside the controller.Edit in response to comments:
Wherever you need to place the URLs, this “URL builder” you are looking for is still the
UrlHelper. You just need to pass it (or the generated URLs) where you need it, being it inside the controller, view or custom helper.To get the links inside the unsorted list HTML structure you mention, you need to put anchors inside the list items like this:
Then again you just need to get the URLs from somewhere and that would be from
UrlHelper.