151

I keep getting this error.

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

the code I have is:

Uri uri = new Uri(slct.Text);
if (DeleteFileOnServer(uri))
{
    nn.BalloonTipText = slct.Text + " has been deleted.";
    nn.ShowBalloonTip(30);
}

Update: the content in slct.Text is ftp.jt-software.net/style.css.

What gives? How is that not a valid URI format? It's plain text.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • It is telling me the content of slct.Text is not a valid Uri. But it is. –  May 22 '10 at 11:46
  • 1
    @j-t-s: could you post the offending URI perhaps? – Mitch Wheat May 22 '10 at 11:48
  • Do you get an exception when creating the Uri with new Uri or when trying to delete the file on the server? – Simon May 22 '10 at 11:49
  • 2
    the offending uri is: ftp.jt-software.net/style.css –  May 22 '10 at 12:50
  • 4
    @j-t-s, I assume those downvotes were coming from the people that did leave a comment and found out that you didn't update your answer. Note, you can edit your question to make it complete (and accept answers), to keep SO clean. You were missing `ftp://` or `http://` in your URI. You get my vote now ;-) – Abel Jul 30 '10 at 11:52
  • Yeah, there's a good reason it'[s missing that... Because my program automatically adds it for me :) But thank you :) –  Jul 30 '10 at 22:56
  • i had same issue, i was missing http:// protocol at the begin of URI – Hassan Faghihi Mar 04 '19 at 08:28

8 Answers8

161

It may help to use a different constructor for Uri.

If you have the server name

string server = "http://www.myserver.com";

and have a relative Uri path to append to it, e.g.

string relativePath = "sites/files/images/picture.png"

When creating a Uri from these two I get the "format could not be determined" exception unless I use the constructor with the UriKind argument, i.e.

// this works, because the protocol is included in the string
Uri serverUri = new Uri(server);

// needs UriKind arg, or UriFormatException is thrown
Uri relativeUri = new Uri(relativePath, UriKind.Relative); 

// Uri(Uri, Uri) is the preferred constructor in this case
Uri fullUri = new Uri(serverUri, relativeUri);
CJBrew
  • 2,720
  • 1
  • 20
  • 27
65

Check possible reasons here: http://msdn.microsoft.com/en-us/library/z6c2z492(v=VS.100).aspx

EDIT:

You need to put the protocol prefix in front the address, i.e. in your case "ftp://"

Simon
  • 9,255
  • 4
  • 37
  • 54
  • 4
    Nothing wrong at all. I was doing everything really quickly, and I accidently clicked the down button, and it said that unless the question is edited i cannot undo it, so if you could kindly edit your question or something, i can re-upvote you :) So sorry about that :-( I didn't mean to downvote you –  May 25 '10 at 17:49
  • voting up back instead of j-t-s. Since the latter disappeared :) – Andy Jun 05 '10 at 17:37
  • 2
    @Simon, I just voted you UP again, sorry about the HUGE delay! You can punch me 2times lol –  Sep 12 '10 at 05:18
  • @Simon, I have the same problem, but my protocol prefix is followed by the port number (8080) in the address bar of the browser, i.e. the complete URI would be: "Http://localhost/8080/api/nameOfMyRestController". How can I manage this? – franz1 Nov 23 '20 at 15:18
20

Better use Uri.IsWellFormedUriString(string uriString, UriKind uriKind). http://msdn.microsoft.com/en-us/library/system.uri.iswellformeduristring.aspx

Example :-

 if(Uri.IsWellFormedUriString(slct.Text,UriKind.Absolute))
 {
        Uri uri = new Uri(slct.Text);
        if (DeleteFileOnServer(uri))
        {
          nn.BalloonTipText = slct.Text + " has been deleted.";
          nn.ShowBalloonTip(30);
        }
 }
Ashish Gupta
  • 14,869
  • 20
  • 75
  • 134
14

Sounds like it might be a realative uri. I ran into this problem when doing cross-browser Silverlight; on my blog I mentioned a workaround: pass a "context" uri as the first parameter.

If the uri is realtive, the context uri is used to create a full uri. If the uri is absolute, then the context uri is ignored.

EDIT: You need a "scheme" in the uri, e.g., "ftp://" or "http://"

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
12

I worked around this by using UriBuilder instead.

UriBuilder builder = new UriBuilder(slct.Text);

if (DeleteFileOnServer(builder.Uri))
{
   ...
}
5

The issue for me was that when i got some domain name, i had:

cloudsearch-..-..-xxx.aws.cloudsearch... [WRONG]

http://cloudsearch-..-..-xxx.aws.cloudsearch... [RIGHT]

hope this does the job for you :)

1

I was getting a debugging error like this while trying to set up Docker.

I've set the docker setting in "launchSettings.json" as follows.

Problem has solved.

"Docker":
{
    "commandName": "Docker",
    "launchBrowser": true,
    "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/",
    "publishAllPorts": false,
    "useSSL": true
}
0

Please check the xml comment file exists or not if you enable SwaggerDoc for the app.

user3724031
  • 195
  • 2
  • 7