【发布时间】:2014-06-09 15:33:53
【问题描述】:
我们有一个 MVC Web 应用程序,它使用 System.Net.HttpClient.PostAsJsonAsync 对 Web 服务进行服务器端调用。
当 web 服务在 IIS 中作为根站点运行时,这可以正常工作。但是当我们在 IIS 中将其配置为虚拟目录时,System.Net.HttpClient.PostAsJsonAsync 会发布到错误的 URL。
using (var client = new HttpClient())
{
var webServiceUrl = ConfigurationManager.AppSettings["WebServiceUrl"];
if (webServiceUrl == null)
throw new Exception("WebServiceUrl not set in web.config");
client.BaseAddress = new Uri(webServiceUrl);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response =
await client.PostAsJsonAsync("api/Authentication", loginModel);
if (response.IsSuccessStatusCode)
...
当我在调试器中跟踪它时,我看到 webServiceUrl 设置为域+虚拟目录(即http://mydomain.com/myvirtualdirectory)。
然后当我收到响应时,它有一个 StatusCode 404,“未找到”。事情是这样的——响应对象包含 RequestMessage,请求中的 URL 不包含虚拟目录。
我们从“http://mydomain.com/myvirtualdirectory”的 BaseAddress 和“api/Authentication”的 RequestUri 开始,我在 RequestMessage 中看到的是“http://mydomain.com/api/Authentication”。虚拟目录已被剥离。
问题是,为什么?
【问题讨论】:
-
在回答之前阅读问题。
标签: c# asp.net-mvc