Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly encode links to external URL in MVC Razor

Tags:

This view suppose to show a list of hyperlinks, each pointing to an external URL. The goal is for the user to click one of these links and have their browser open a new tab with the selected URL. Currently I have the following markup:

@Html.ActionLink("SomeSite", "http://subdomain.mydomain.com/SomeSite") 

This markup produces:

http://localhost:58980/AccessInstance/http%3a/subdomain.mydomain.com/SomeSite 

instead of :

http://subdomain.mydomain.com/SomeSite 

What can I change in my markup to make this work as I expect?

like image 503
Eugene Goldberg Avatar asked Apr 15 '14 20:04

Eugene Goldberg


1 Answers

You don't need to use @Html.ActionLink for that. Just use a plain A tag:

<a href="http://subdomain.mydomain.com/SomeSite">SomeSite</a> 

Html.ActionLink is specifically for generating links to actions defined in MVC controllers, in the same app. Since you're linking to an absolute URL, you don't need any of the functionality that Html.ActionLink provides.

like image 91
rossipedia Avatar answered Sep 27 '22 22:09

rossipedia