c# - ASP.NET Web API 在 IIS 中发布时不起作用?

标签 c# asp.net-web-api

我已经构建了一个 ASP.NET Web API,当我从 Visual Studio 运行或浏览它时,它工作正常,但是当我将它托管在 IIS 中并尝试浏览它时,它不起作用,并给出以下错误:

HTTP 400 error It’s not you, it’s this link (it appears to be broken)

这里是我发布网络服务的地方 enter image description here

API方法是

 [HttpGet]
    [Route("findall")]
    public HttpResponseMessage findAll()
    {
        try
        {
            var response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new StringContent(JsonConvert.SerializeObject(mde.Products.ToList()));
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            return response;

        }
        catch
        {
            return new HttpResponseMessage(HttpStatusCode.BadRequest);
        }
    }

我向它发送以下请求:

http://localhost:9810/api/product/findall

如何解决这个问题?

最佳答案

根据屏幕截图,您已将应用程序部署为默认站点上的虚拟应用程序。 /myserver 被定义为虚拟路径。这意味着您的 API 根 URL 将是:

http://localhost:{IIS_PORT}/myserver/{YOUR_APP_ROUTES}

默认网站端口为80;如果您帖子中的 URL ( http://localhost:9810/api/product/findall ) 在 IIS-express 上正常工作,请尝试为您的 IIS 部署调用此 URL:

http://localhost/myserver/api/product/findall

关于c# - ASP.NET Web API 在 IIS 中发布时不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53848226/

相关文章:

http - 使用文件和 xml 内容发出 POST 请求

c# - 如何获取属性中的 Controller 端点信息?

c# - SSL TcpClient C# .Net Compact Framework 1.0

c# - 将实体对象转换为 IEnumerable

C# Linq to XML,当 child 满足条件时获取 parent

c# - DateTime.Now C# 向数据库添加记录时的问题

c# - jpeg 的色度子采样算法

asp.net-mvc-4 - 如何开发 ASP.NET Web API 来接受复杂对象作为参数?

c# - 如何将本地数据库上传到 Windows Azure?

c# - 反射加载类型的方法未被调用