How to properly encode links to external URL in MVC Razor
Asked Answered
C

6

22

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?

Clan answered 15/4, 2014 at 20:45 Comment(0)
U
41

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.

Unforgettable answered 15/4, 2014 at 20:58 Comment(2)
Hello I call the url from a database field and I need to be link. I call the data in my view @Html.DisplayFor(model => model.Announcment_Link) how can I make it to be a linkProp
Additionally, if you generate the href from an entity property, MVC will generate an action URL unless the address specifies the http protocol ie: stackoverflow.com vs www.stackoverflow.com . So make sure that you got that http in your data if you want an external url.Kongo
A
16

Two ways :

1. update the database column with full link:

eg SQL:

update ProductTable set ProductLink='http://www.example.com/Product/Mobiles' where ID=123

In asp mvc view

<a href="@model.ProductLink">View</a>

2. Hardcode the http part and list from model

<a href="http://@model.ProductLink">View</a>

Hope helps someone.

Alicia answered 22/7, 2015 at 16:4 Comment(4)
When I use <a href="http://@model.ProductLink">View</a> it removes the : from httpProp
@touinta, some browser dont show up the protocol check this article , may i know how you store the example link in your database column ?Alicia
@storm In the database the Link field is varchar. Some of them are with www and other are with www.Prop
why dont you store with http:// ? then it would be easy, check my answer for your question here and comment there.Alicia
M
1

Using .NET Core 6

This seems to be the most correct answer:

 <a [email protected] target="_blank">Link</a>

This will generate the following result:

enter image description here

As you can see at the bottom left corner of the window before clicking the link, the URL address was rendered as it is (NOTE: The cursor was recorded out of place for some reason, that's a ShareX problem, ignore it).

Than link will be directly saved as a nvarchar(750) type (probably any character like type will do the work). No changes to the original link were made before saving it or on reading:

enter image description here

Magellan answered 7/11, 2022 at 23:41 Comment(0)
U
0

While a ViewBag is overused and not the best choice most of the time this is something that I had done when inheriting someone else's mvc app to do a quick fix for a URL that I needed to redirect to with a specific dynamically changing querystring parameter

  <a target="_parent" href="http://localhost:56332/services/@ViewBag.factory">View Service</a>
Unrefined answered 28/8, 2015 at 20:26 Comment(0)
C
-1

You need to take into account your RouteConfiguration.

routes.MapRoute( name: "Default", url: "{controller}/{action}"

because you are specifying the action link as the entire link that you want to redirect. I would recommend that you use the @rossipedia answer because you can make tricky things like putting a span inside the link

Cyanine answered 19/5, 2015 at 14:48 Comment(0)
A
-1

Here to display link that are clickable in index page

     <td>         
        @Html.ActionLink(item.FileName, "../Uploads/Catalogue/"+item.FileName)
    </td>
Anthe answered 10/11, 2017 at 10:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.