7

I'm unable to get Html.ActionLink to produce absolute urls.

Html.ActionLink(DataBinder.Eval(c.DataItem, "Name").ToString(), DataBinder.Eval(c.DataItem, "Path").ToString())

This pulls the data from my model correctly, but appends the path to the end of the current page, producing URLs like "http://localhost:24590/www.google.com"

How can I get this to work how I want it to?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
Skrealin
  • 1,114
  • 6
  • 16
  • 32

2 Answers2

17

This works for me:

<a href="http://@Model.URL">
    Click Here
</a>
Nick Howard
  • 943
  • 1
  • 13
  • 25
  • 1
    This is the correct answer. When attempting to link to an EXTERNAL resource (i.e., a different website or file not in your project), there is no need for the @Html.ActionLink helper, which is only for generating RELATIVE links to routes in your project. – Synctrex Oct 16 '19 at 14:05
6

Use an absolute URL starting with i.e. http://.

<a href="www.google.com"></a>

would have the same result, because it's a relative url.

DanielB
  • 19,910
  • 2
  • 44
  • 50
  • not sure how this answers the question. I am running into the same issue and not sure how to generate a url with an absolute path. For example, I've attempted to do something like Html.ActionLink("test link", "http://www.google.com") and it generates what Skrealin is complaining about. Then, I tried test link and it yielded the same result. So, how can this be done? – clockwiseq Nov 20 '11 at 22:51
  • 4
    As i wrote in the answer, use an absolute URL. The ActionLink helper tries to provide a URL for your website, so if you give a relative path, he has to think it's part of your page. You have to explicitly set an absolute URL and this is done by having i.e. `http://` in front. You should use `Html.ActionLink("test link", "http://google.com")`. – DanielB Nov 21 '11 at 09:08
  • This is incorrect.Html.ActionLink() only generates paths relative to the current project, so that would tack on "google.com" to the end of the current host/URI path. – Synctrex Oct 16 '19 at 14:07