[英]C# web application causes 404 error in Azure when deployed from one computer, but not from another
[英]404 when calling a c# web api from another c# web forms application
我创建了一个简单的web api,当我从IE访问它时,web api正好返回我的预期。 但是,现在我尝试使用httpclient从Web表单应用程序访问相同的Web API。 但现在我收到404错误。 但api似乎有效,因为我在使用浏览器时确实收到了结果。 任何想法出了什么问题? 这是代码:
HttpClientHandler handler = new HttpClientHandler()
{
UseDefaultCredentials = true
};
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("http://server/appdir");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("/api/environment").Result;
这是有效的代码。
HttpClientHandler handler = new HttpClientHandler()
{
UseDefaultCredentials = true
};
HttpClient client = new HttpClient(handler);
client.BaseAddress = new Uri("http://eetmws10v");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("/aim2/api/environment").Result;
我在2天内处理这个问题,对我来说有用的是添加自定义用户代理,就像在这个链接中一样
码
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");
哇。 本周末我遇到了同样的错误,我无法想象这个问题的原因是多么脆弱。
这对我有用:
string _baseAddress = "http://localhost/webapi/";
string controllerURL = "api/School";
client.BaseAddress = new Uri(_baseAddress);
string result = client.GetStringAsync("controllerURL").Result.ToString();
_baseAddress和controllerURL的组合导致我的404错误如下:
string _baseAddress = "http://localhost/webapi/";
string controllerURL = "/api/School";
要么
string _baseAddress = "http://localhost/webapi";
string controllerURL = "api/School";
要么
string _baseAddress = "http://localhost/webapi";
string controllerURL = "/api/School";
我真诚地希望尽快采取措施消除这种不必要的僵化,这可能会浪费许多开发人员有用的工时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.