ASP.NET MVC 在 TEST 服务器中抛出以下错误,我无法在 Developer 机器上重现该错误。
尝试从 MVC 连接到 WebAPI 服务并收到错误
只允许“http”和“https”方案。参数名称:requestUri
我无法从其他问题中得到答案,因为他们主要是在谈论 WCF 绑定(bind)。
代码:
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;
注意:通过浏览器访问 WebAPI URL 返回数据。
最佳答案
考虑到您的 URL
参数中可能包含或不包含的内容,我猜它不是以 http://
或 https 开头://
。为此,它必须从这两者之一开始。
因此,如果您的 URL
当前读取(假设 URL
是一个 string
):
mycompany.com/myservices
它需要更改为:
http://mycompany.com/myservices
或者
https://mycompany.com/myservices
如果 URL
不是 string
,而是 System.Uri
的一个实例,那么您需要做类似的事情创建此对象时,或设置 URL.Scheme
。
关于ASP.NET MVC 4 到 ASP.NET Web API - 错误 : Only 'http' and 'https' schemes are allowed. 参数名称:requestUri,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18211799/