如何在 ASP.NET 核心 MVC 应用程序中使用 Web API 核心

How to consume Web API Core in ASP.NET Core MVC App

我正在尝试在 MVC 应用程序中使用我的 Web API 核心服务,该服务托管在我本地计算机上的 IIS 10 中,但是,我收到 404 not found 错误,而当我在浏览器地址中输入 URL 时它起作用了。

这是我的控制器代码

public IActionResult Index()
{
    using (var client = new HttpClient())
    {
        //Construct API URI
        client.BaseAddress = new Uri("http://localhost");
        var contentType = new MediaTypeWithQualityHeaderValue("application/json");
        client.DefaultRequestHeaders.Accept.Add(contentType);
        var response = client.GetAsync("/api/DeviceType/devgetall").Result;
        var stringData = response.Content.ReadAsStringAsync().Result;
        var data = JsonConvert.DeserializeObject<List<DeviceType>>(stringData);
        var deviceTypes = _mapper.Map<List<DeviceTypeModel>>(data);
        var modelList = new DeviceTypeModelList {DeviceTypeModels = deviceTypes};
        return View(modelList);
    }
}

当我在 GetAsync 方法中输入完整的 link 并删除 BaseAddress 属性.

时,它现在可以工作了