为什么 httpResponse.IsSuccessStatusCode 返回为 false?从 API 到 ASP.NET Core MVC?

Why is httpResponse.IsSuccessStatusCode returned as false? From an API to ASP.NET Core MVC?

我有 3 层项目,我在 ASP.NET 核心上做数据库/API/MVC;所有控制器都正常工作,但是这个 API 控制器的连接不工作。

这是我的业务层函数:

    public List<Ticket> ViewTickets()
    {
        try
        {
            using (var db = new MaintenanceSysContext(_options))
            {
                List<Ticket> tickets = null;
                List<int> ticketsId = db.Tickets.SelectMany(t => t.backOfficesTickets).Where(u => u.BackOfficeId == _backOfficeEntry.GetUserId()).Select(t => t.TicketId).ToList();
              
                foreach( var i in ticketsId)
                {
                    tickets = db.Tickets.Where(t => t.Id == i).ToList();
                }

                if (tickets != null)
                {
                    return tickets;
                }
                else
                {
                    return null;
                }
            }
        }
        catch (Exception)
        {
            throw;
        }
    }

这是 API 中的控制器:

    [HttpGet]
    [Route("[action]")]
    [ActionName("ViewTickets")]
    public IActionResult  ViewTickets()
    {
        var tickets = _buildingManager.ViewTickets();
        return Ok(tickets);
    }

这是我的 ASP.NET Core MVC httpClient 连接:

url基数:

    HttpClient client = new HttpClient();

    public static string baseUrl = "http://localhost:16982/api/BuildingManagerAPI/";

ASP.NET 核心 MVC 控制器:

[HttpGet]
public async Task<ActionResult> GetTickets()
{
    List<Ticket> TicketList = new List<Ticket>();

    using (client)
    {
        var accessToken = HttpContext.Session.GetString("Token");
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
        var httpResponse = await client.GetAsync(baseUrl + "ViewTickets");

        if (httpResponse.IsSuccessStatusCode)
        {
            TicketList = await httpResponse.Content.ReadAsAsync<List<Ticket>>();
        }
    }
    
    return Json(TicketList, System.Web.Mvc.JsonRequestBehavior.AllowGet);
}

httpResponse.IsSuccessStatusCode 中,我得到了 false 的值,而 API 工作正常并且 returns 在 Postman 中测试时得到了正确的响应。我不知道我的连接有什么问题。

更新:

邮递员请求输出:

[
{
    "Id": 1,
    "BeneficiaryID": 4,
    "BeneficiaryUser": null,
    "backOfficesTickets": null,
    "StatusID": 2,
    "status": null,
    "ApprovalState": 2,
    "Date": "2021-08-22T00:00:00",
    "Picture": null,
    "MaintenanceTypeID": 1,
    "maintenanceType": null,
    "Description": "krng",
    "BuildingManagerComment": null,
    "FloorId": 1,
    "floor": null,
    "IsCancelled": false,
    "CancellationReasonID": null,
    "cancelationReason": null,
    "RejectedBy": null,
    "UserRejected": null,
    "RejectionReason": null,
    "CreatedBy": 4,
    "CreatedTime": "0001-01-01T00:00:00",
    "UpdatedBy": 0,
    "UpdatedTime": "0001-01-01T00:00:00",
    "IsDeleted": false
}

Red Point 未获取 uri(客户端基础 uri=null),然后 IsSuccessStatusCode 设置为 NULL。

尝试添加接受header

....
 client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

问题出在令牌中(不包含声明),我们已修复它。谢谢大家。