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

前端 未结 6 1708
悲哀的现实
悲哀的现实 2020-11-27 14:34

I keep getting this error.

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

the code I have is:

Uri u         


        
相关标签:
6条回答
  • 2020-11-27 15:17

    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);
    
    0 讨论(0)
  • 2020-11-27 15:20

    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://"

    0 讨论(0)
  • 2020-11-27 15:20

    I worked around this by using UriBuilder instead.

    UriBuilder builder = new UriBuilder(slct.Text);
    
    if (DeleteFileOnServer(builder.Uri))
    {
       ...
    }
    
    0 讨论(0)
  • 2020-11-27 15:21

    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://"

    0 讨论(0)
  • 2020-11-27 15:25

    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 :)

    0 讨论(0)
  • 2020-11-27 15:26

    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);
            }
     }
    
    0 讨论(0)
提交回复
热议问题