3

Firstly, this seems like something that should have been asked before, but I cannot find anything that answers my question.

A basic overview of my task is to render an anchor link on a web page which is based on a user defined web address. As the address is user defined this could be in any format, for example:

What I need to do with this value is to set it as the href property of an anchor tag. Now, the problem is that (in Chrome at least) only the first two examples will work due to the fact they are recognised as absolute URL paths. The last two examples will redirect to the same domain (i.e. treated as relative paths)

So the ultimate question is: What is the best way to format these values to ensure a consistent absolute path is used? I could check for http/https and add it if missing, but I was hoping there might be an out of the box .Net class that would be more reliable.

In addition, as this is a user defined value, it could be complete junk anyway so a function to validate the URL would be a nice bonus too.

musefan
  • 47,875
  • 21
  • 135
  • 185

2 Answers2

1

We ran into this problem a few months back, and needed a consistent way of ensuring the URLs were absolute. We also wanted a way of removing http(s):// for displaying the URL on the web page.

I came up with this function:

public static string FormatUrl(string Url, bool IncludeHttp = null)
{

    Url = Url.ToLower();

    switch (IncludeHttp) {
        case true:
            if (!(Url.StartsWith("http://") || Url.StartsWith("https://")))
                Url = "http://" + Url;
            break;
        case false:
            if (Url.StartsWith("http://"))
                Url = Url.Remove(0, "http://".Length);
            if (Url.StartsWith("https://"))
                Url = Url.Remove(0, "https://".Length);
            break;
    }

    return Url;

}

I know you're after an "out of the box" library, but this may be of some help.

I think the problem with an "out of the box" solution would be that the function won't know whether the URL should be http:// or https://. With my function I've made an assumption that its going to be http://, but for some URLs you need https://. If Microsoft were to build something like this into the framework, it would be buggy from the start.

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • Is there a reason you are using the bitwise OR instead of the logical OR in the first `if`? See [this answer](http://stackoverflow.com/questions/7331686/why-and-not/7331721#7331721) for an explanation of the differences. – Daniel Hilgarth Apr 18 '12 at 10:16
  • @DanielHilgarth I've updated the answer. The original script was written in VB.NET, so I've posted it in c# for the OP, but missed that. – Curtis Apr 18 '12 at 10:17
  • Thanks for the suggestion, it looks like I may have to opt for a manual implementation (at least for now) but the `https` part is not an issue for me. For those types of sites I would expect the user to have explicitly included the https part anyway – musefan Apr 18 '12 at 10:30
0

You can try using this overload of the Uri class:

Uri Constructor (String)

This constructor creates a Uri instance from a URI string. It parses the URI, puts it in canonical format, and makes any required escape encodings.

This constructor does not ensure that the Uri refers to an accessible resource.

This constructor assumes that the string parameter references an absolute URI and is equivalent to calling the Uri constructor with UriKind set to Absolute. If the string parameter passed to the constructor is a relative URI, this constructor will throw a UriFormatException.

This will try to construct a canonical Uri from the user input. And you have lots of properties to check and extract the URL parts that you need.

Community
  • 1
  • 1
JotaBe
  • 38,030
  • 8
  • 98
  • 117
  • Thanks for the suggestion, but after trying this it only works for the first two examples. An exception is thrown for the last two examples. So this will not work for my needs – musefan Apr 18 '12 at 10:28
  • Then you should try a combination of both answers. With the other answer you satisfy the requisite of having http. With the second, you verify that the rest of the URL is correct. Can you check the user input to force to include the http(s)? – JotaBe Apr 18 '12 at 10:36
  • I could force a valid url on entry, but would still want to do the check prior to use anyway (just to be sure). I will attempt a custom function for now and see how it goes – musefan Apr 18 '12 at 10:41
  • Use a regex to force it. Look for "c# regex url checking" in google. There are lot of good samples. – JotaBe Apr 18 '12 at 11:07