首页 > 解决方案 > 无法从 Rest API 获取数据 - HTTP ERROR 500 - C#

问题描述

我想用 customerId 和 orderId 获取数据,如下所示:

[HttpGet("customers/{id}/orders/{orderId}")]

但是当我通过 URL 更改 customerId 时,我得到了另一个与当前客户不对应的 orderId。我希望 customerId 和 orderId 是正确的,而不是从另一个客户那里获取订单。

另外,这个路由器:

[HttpGet("customers/{id}/orders")]

这会从所有客户那里获取所有订单。我只想要给定的 customerId 来为给定的 customerId 带来所有特定订单。

我仍在尝试掌握这种编程逻辑并且对此仍然很陌生。抱歉,如果这看起来很愚蠢,但我已经尝试了很长时间没有运气的解决方案。非常感谢您的指导和帮助。

这是我到目前为止所拥有的:

楷模:

客户.cs:

using System.Collections.Generic;

namespace Project.Models
{
    public class Customer
    {
       public string id { get; set; }
       public string firstName { get; set; }
       public string lastName { get; set; }
       public string dateOfBirth { get; set; }
    }
} 

订单.cs:

using System.Collections.Generic;

namespace Project.Models
{
   public class Order
   {
       public string orderId { get; set; }
       public string product { get; set; }
       public string status { get; set; }
       public double price { get; set; }
   }
}

存储库:

using System.Collections.Generic;
using System.Linq;
using Project.Models;

namespace Project.Repositories
{
   public class OrderRepository
   {

       private static Dictionary<int, Order> ORDERS
                                = new Dictionary<int, Order>();

       static OrderRepository()
       {
           ORDERS.Add(1, new Order
           {
               orderId= "124",
               product= "Shirts",
               status= "On it's way",
               price= 100.20,
           });

           ORDERS.Add(1, new Order
           {
               orderId= "122",
               product= "Pants",
               status= "Not ready",
               price= 300.30,
           });

           ORDERS.Add(2, new Order
           {
               orderId= "143",
               product= "Deadpool",
               status= "On it's way",
               price= 6.20,
           });

           ORDERS.Add(2, new Order
           {
               orderId= "156",
               product= "Socks",
               status= "Not ready",
               price= 3.30,
           });
     }

     public static Order GetByOrderid(int id)
     {
        if (ORDERS==null || ORDERS.Count<1)
          return new Order();
        return ORDERS[id];
     }

     public static List<Order> GetAllOrders()
     {
        if (ORDERS==null || ORDERS.Count<1)
          return new List<Order>();
        return ORDERS.Values.ToList();
     }
}

控制器:

订单控制器.cs:

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Project.Models;
using Project.Repositories;

namespace Project.Controllers
{
   public class OrderController : Controller
   {
      [HttpGet("customers/{id}/orders/{orderId}")]
      public Order FindOneOrder(int id)
      {   
          return OrderRepository.GetByOrderid(id);
      }

      [HttpGet("customers/{id}/orders")]
      public List<Order> GetCustomerOrders()
      {
          return OrderRepository.GetAllOrders();
      }
    }
 }

标签: c#.net

解决方案


问题 1 - 路由值

您需要将路由中的参数用作操作方法中的参数。

[HttpGet("customers/{customerId}/orders/{orderId}")]
public Order FindOneOrder(int customerId, int orderId) //including orderId
{   
    return OrderRepository.GetByOrderid(customerId, orderId);
}

[HttpGet("customers/{customerId}/orders")]
public List<Order> GetCustomerOrders(int customerId) // using customerId
{
    return OrderRepository.GetAllOrders(customerId);
}

我将“id”修改为customerId,应该更容易理解和维护。

您将需要修改存储库方法以接受这些新参数。

问题 2 - 字典创建

正如jdweng 所说,您正在使用相同的键创建字典条目。字典条目必须具有唯一值。

ORDERS.Add(1, new Order
{
   orderId= "124",
   product= "Shirts",
   status= "On it's way",
   price= 100.20,
});

ORDERS.Add(2, new Order
{
   orderId= "122",
   product= "Pants",
   status= "Not ready",
   price= 300.30,
});

ORDERS.Add(3, new Order
{
   orderId= "143",
   product= "Deadpool",
   status= "On it's way",
   price= 6.20,
});

ORDERS.Add(4, new Order
{
   orderId= "156",
   product= "Socks",
   status= "Not ready",
   price= 3.30,
});

请注意,这些调用的第一个参数总是不同的。

问题 3 - 首先使用字典

我将不再假设字典的键是客户 ID。这不是一个好主意。一个客户可以有多个订单,但您不能将这些数据保存在字典中,因为您不能重用该键。

相反,让我们修改 Order 对象以包含 CustomerId:

public class Order
{
   public string CustomerId { get; set; }
   public string OrderId { get; set; }
   public string Product { get; set; }
   public string Status { get; set; }
   public double Price { get; set; }
}

属性以大写字母开头

现在在我们的存储库中,我们将使用 aList<Order>来保存订单。

private static List<Order> orders;

static OrderRepository()
{
    orders = new List<Order>();

    orders.Add(new Order
    {
        CustomerId = "1",
        OrderId = "1",
        // other properties
    });

    orders.Add(new Order
    {
        CustomerId = "1",
        OrderId = "2",
        // other properties
    });

    // ...etc
}

以及我们的搜索方法:

public static Order GetByOrderid(int customerId, int orderId)
{
    return orders
        .Where(x => x.CustomerId == customerId)
        .Where(x => x.OrderId == orderId)
        .SingleOrDefault();
    // remember that the caller needs to check for null value
}
public static List<Order> GetAllOrders(int customerId)
{
    return orders.Where(x => x.CustomerId == customerId).ToList();
}

推荐阅读