簡體   English   中英

如何在ASP.NET Core MVC App中使用Web API Core

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

我正在嘗試使用在MVC應用程序的本地計算機上的IIS 10托管的Web API核心服務,但是我遇到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方法中鍵入完整鏈接並擺脫了BaseAddress屬性時,它就可以工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM