【问题标题】:How to consume Web API Core in ASP.NET Core MVC App如何在 ASP.NET Core MVC App 中使用 Web API Core
【发布时间】:2017-11-01 15:38:31
【问题描述】:

我正在尝试使用我的 Web API 核心服务,该服务托管在我的本地计算机上的 IIS 10 中浏览器地址。

这是我的控制器代码

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);
    }
}

【问题讨论】:

  • 能否请您分享一下您是如何使用它的?
  • 对不起,我没听懂。
  • 我正在尝试通过我的问题中附加的代码来使用它。
  • 我的意思是你的调用应该命中的代码。
  • 在这些情况下,很多时候问题与动词(GET、POST、...)或请求的参数有关。

标签: asp.net-mvc iis asp.net-web-api hosting .net-core


【解决方案1】:

当我在 GetAsync 方法中键入完整链接并摆脱 BaseAddress 属性时,它现在可以工作了。

【讨论】:

    猜你喜欢
    • 2021-09-16
    • 1970-01-01
    • 2017-07-02
    • 2018-12-30
    • 2022-01-02
    • 2021-08-22
    • 1970-01-01
    • 2020-12-13
    • 2016-12-17
    相关资源
    最近更新 更多