【问题标题】:Server side call apiController gives 404 with iis7服务器端调用 apiController 用 iis7 给出 404
【发布时间】:2012-11-29 05:13:43
【问题描述】:

这是我的代码:

1 HttpClient client = new HttpClient();
2 client.BaseAddress = new Uri("http://localhost/");
3 HttpResponseMessage message = client.GetAsync("api/MyAPI").Result;
4 IEnumerable<string> supplies = 
                 message.Content.ReadAsAsync<IEnumerable<string>>().Result;
5 return View(supplies);

并且错误发生在第 3 行 Error:404

但是当我使用 Visual Studio 2010 开发服务器时,没有出现错误。

【问题讨论】:

  • 它仅根据您托管它的方式返回 404。也许你把它放在 IIS 的虚拟目录中?
  • 是的,我放在一个虚拟目录下,虚拟路径是:'/WebAPITest',vs项目url:'localhost/WebAPITest'

标签: asp.net-mvc iis-7 asp.net-web-api


【解决方案1】:

基本上你必须自己处理这里的情况。

对于开发,您的 API 的路径是:

http://localhost/api/MyAPI

对于 IIS 上的生产来说,它是:

http://localhost/WebAPITest/api/MyAPI

最简单的方法,实际上是我们在我公司所做的,是将基本路径放入配置(可能是应用程序设置)中,然后执行以下操作:

Settings.BaseUrl = "http://localhost/";
client.BaseAddress = new Uri(Settings.BaseUrl);

部署到 IIS 时,将 Settings.BaseUrl 更改为 http://localhost/WebAPITest/

理想情况下,您还可以删除 VS 开发服务器并在 IIS 上进行调试。 (这需要以管理员权限运行 VS)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多