0

I have this url: turnerstadium.co.il, I'm trying to check if the scheme is correct so I did:

if (!Uri.CheckSchemeName(link))
{
   link = "http://" + link;
}

the problem is that CheckSchemeName return true, so when I do this:

var url = new Uri(link);

I get:

Invalid URI: The format of the URI could not be determined

how can I fix that?

zparo
  • 55
  • 7

2 Answers2

0

This is the code of CheckSchemeName method.

public static bool CheckSchemeName(string schemeName)
{
  if (schemeName == null || schemeName.Length == 0 || !Uri.IsAsciiLetter(schemeName[0]))
    return false;
  for (int index = schemeName.Length - 1; index > 0; --index)
  {
    if (!Uri.IsAsciiLetterOrDigit(schemeName[index]) && schemeName[index] != '+' && (schemeName[index] != '-' && schemeName[index] != '.'))
      return false;
  }
  return true;
}

From the remarks section of Uri.CheckSchemeName:

The scheme name must begin with a letter and must contain only letters, digits, and the characters ".", "+", or "-".

So as you can see, this method only checks if the string you are passing meets these requirements.

If you only want to check if the string starts with "http://" and append it in case it is not there, one of the possible solutions would be:

  if (!link.StartsWith("http://"))
  {
    link = "http://" + link;
  }

Otherwise I recommend reading this.

Mikołaj Mularczyk
  • 959
  • 12
  • 20
-1

Check UriBuilder at: https://learn.microsoft.com/en-us/dotnet/api/system.uribuilder?view=netframework-4.7.2

It has constructors to add the scheme if the scheme is not present.

var url = new UriBuilder("siteUrl.com").Uri.ToString();

This will return "http://siteUrl.com"

Manoj Choudhari
  • 5,277
  • 2
  • 26
  • 37