System.UriFormatException: Invalid URI : The URI is Empty

29,976

You posted in the comments that your URL looks like services.odata.org/Northwind/Northwind.svc/…. You are missing the protocol from the URL and the Uri constructor requires it. Try changing it to this:

var url = "http://services.odata.org/Northwind/Northwind.svc/Employees?$filter=minute%28BirthDate%29";
client.BaseAddress = new Uri(url);

Note the http:// - that is the protocol. You might also have to remove the arguments from the URL, but you can quickly test for that.

Share:
29,976
Novak007
Author by

Novak007

Hi. Thanks SO guys for your help.

Updated on May 19, 2020

Comments

  • Novak007
    Novak007 almost 4 years

    I am using a URL that works when I put it in chrome, but in my VS C# code the same url when used in (public async static task)

    using (var client = new HttpClient(handler))  {    
    client.BaseAddress = new Uri(url);    ----------> Error
    

    ....

    Method threw exception: 
    System.AggregateException: One or more errors occurred. ---> System.UriFormatException: Invalid URI: The URI is empty.
    Result StackTrace:  
    at System.Uri.CreateThis(String uri, Boolean dontEscape, UriKind uriKind)
       at System.Uri..ctor(String uriString)
    

    I am sure the URL isn't malformed, so I can't figure out what could be the reason for this error . Thanks for your help.

  • Novak007
    Novak007 almost 9 years
    actually i used it but still i am getting the error.
  • Novak007
    Novak007 almost 9 years
    i placed my url in app.config by creating a key value pair and am then accessing it in a class using configuration manager.appsettings . i guess this is a fine way to access it, but empty url means i can't so could you please give a hint as to what might have been wrong
  • Jackdon Wang
    Jackdon Wang almost 2 years
    I fixed this bug today, because I have not add the key/value to AppSetting.