ASP.NET MVC 4 to ASP.NET Web API - Error: Only 'http' and 'https' schemes are allowed. Parameter name: requestUri

asp.net, asp.net-mvc-4, asp.net-web-api

Solution

In anticipation of what might or might not be in your `URL` parameter, I'm guessing it doesn't start with `http://` or `https://`. For this to work, it must start with one of these two.

So if your `URL` currently reads (assuming `URL` is a `string`):

mycompany.com/myservices

It needs changing to either:

http://mycompany.com/myservices

Or

https://mycompany.com/myservices

If `URL` isn't a `string`, but is instead an instance of `System.Uri`, then you need to do a similar thing when creating this object, or set the `URL.Scheme`.

Problem

ASP.NET MVC throws the following error in TEST Server, which I could not reproduce in Developer machines. Trying to connect to WebAPI Service from MVC and getting the error Only 'http' and 'https' schemes are allowed. Parameter name: requestUri I could not get an answer from other questions since they are mainly talking about WCF bindings. Code: ``` var credentials = "username:Password"; var encoded = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(credentials)); this.client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", encoded); System.Net.Http.HttpClient client = new HttpClient(); HttpResponseMessage response = client.GetAsync(URL).Result; ``` Note: Accessing the WebAPI URL through browser returns the data.

Original source