2

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.

2
  • 2
    Can you please post the exact code you are using the make the call from MVC? Commented Aug 13, 2013 at 14:27
  • It looks as though your issue is the contents of URL, can you update your question to show what URL is being set to? Commented Aug 13, 2013 at 15:10

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

In DEV environment using Visual Studio and IIS Express, I wasn't having any trouble. Once I moved to a IIS server in Test machine and configured as different applications, got this issue. Thank you for your answer and it solved the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.