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

前端 未结 6 1776
悲哀的现实
悲哀的现实 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);
    

提交回复
热议问题