Explicitly treat the link as an absolute link?

I have a control on one of my pages that requires some user input (URL) and allows the user to test the link.

<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("URL") %>' Target="_blank">Click here to test link</asp:HyperLink> 

The problem is that the user places the URL “google.com”, then the link will be processed as “http: //localhost/google.com”. I have to put "http://www.google.com" so that it gets to the right place.

Is it possible to consider a link as if it is an absolute link in all cases?

+7
source share
4 answers

Try it...

 NavigateUrl='<%# Eval("URL", "http://{0}") %>' 

Edit: if you want to add a check to see if it already contains http:// , then it should be like.

 NavigateUrl=<%# Eval("URL").ToString().Contains("http://") == true ? Eval("URL") : "http://" + Eval("URL") %> 
+4
source

Is this an academic question or do you have a problem?

Firstly, the notion that http://www.google.com is the right place and http: //localhost/google.com is wrong.

Your problem, as I see it, is. When you retrieve URLs from a data source, there are raw URLs that the ASP.NET Framework recognizes as a relative URL. But you would like them to use their absolute uri.

But the problem is not in the structure, as http: //localhost/google.com may be a valid URL.
A good example for this would be the builtWith site. It has valid urls like http://builtwith.com/facebook.com

So, the problem is where you enter the data. Why couldn't you place a small combo so that users could easily add protocols and then add a URL to it, as shown below?

A small example of the UI

Hope this helps.

+3
source

Based on your comments:

What if it is a secure (https) site or an FTP site?

and

Will this work? What if http is already in the input? What if it is a secure (https) site or an FTP site?

and

The problem is that the user places the URL “google.com”, then the link will be processed as “http: //localhost/google.com”. I have to put "http://www.google.com" so that it gets to the right place.

Is it possible to consider a link as if it is an absolute link in all cases?

You ask for magic. Here we do not specialize in magic. You must specify something that lets you know that it can become a specific type of domain. Tell them if they don’t specify the protocol you will be like http: // and the rest of the time they need to specify the protocol. That is all you can do.

As for determining whether he received a protocol, check string.Contains("://") , as the browser will do this anyway, more or less.

+2
source

In your code, check if the user contains the input "http: //" and add it if it is not. You can also use JavaScript for this.

+1
source

All Articles