SocketException 和 HttpRequestException

问题描述 投票:0回答:1

我想将我的 asp.net core API 项目连接到我的 asp.net core 项目。另外,我的 Asp.net 核心项目正在准备使用 n 层架构,我在表示层中定义了一个新控制器,并希望使用 get-async 将员工列表拉入其中,但是“var responseMessage = wait httpClient. GetAsync("https://localhost:7087/api/Employee/");"它说我在该部分中有一个错误,并且我在标题中得到了错误。

这是我的控制器:

using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using BlogApiDemo.DataAccessLayer;
using System.Text;

namespace CoreDemo.Controllers
{
    public class EmployeeTestController : Controller
    {
        public async Task<IActionResult> Index()
        {
            var httpClient = new HttpClient();
            var responseMessage = await httpClient.GetAsync("https://localhost:7087/api/Employee/");
            var jsonString = await responseMessage.Content.ReadAsStringAsync();
            var values = JsonConvert.DeserializeObject<List<Class1>>(jsonString); //json convert .net form
            return View(values);
        }

    }

    public class Class1
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
}

这是我的观点:

@using CoreDemo.Controllers
@model List<Class1>
@{
    ViewData["Title"] = "Index";
    Layout = "~/Views/Shared/AdminLayout.cshtml";
}

<table class="table table-bordered">
    <tr>
        <th>#</th>
        <th>Adı Soyadı</th>
        <th>Sil</th>
        <th>Düzenle</th>
    </tr>
    @foreach(var item in Model)
    {
        <tr>
            <td>@item.ID</td>
            <td>@item.Name</td>
        </tr>
    }
</table>

api asp.net-core url asp.net-web-api httprequest
1个回答
0
投票

提供完整的异常,否则无法猜测问题所在。它的范围可能从网络错误到 Cors 问题

© www.soinside.com 2019 - 2024. All rights reserved.