![](/img/trans.png)
[英]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.