【发布时间】:2018-07-09 06:11:30
【问题描述】:
我正在尝试创建新的 customerId 和 orderId。 然后它将显示一个新订单并检索创建的代码 201。 我不明白为什么代码不起作用。我有什么遗漏吗?
为此工作了几天,但没有运气。 希望你能看到我哪里出错了。
仍然是编程新手,非常感谢您的帮助。
代码如下:
型号:
using System.Collections.Generic;
namespace Project.Models
{
public class Customer
{
public string CustomerId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string DateOfBirth { get; set; }
}
public class Order
{
public string OrderId { get; set; }
public string Product { get; set; }
public string Status { get; set; }
public double Price { get; set; }
}
public class CustomerOrder
{
public string CustomerId {get; set;}
public string OrderId { get; set; }
}
}
存储库:
using System.Collections.Generic;
using System.Linq;
using Project.Models;
using System.Net;
using System.Collections.Specialized;
using System.Text;
namespace Project.Repositories
{
public class OrderRepository
{
private static List<Order> _Orders;
private static List<CustomerOrder> _CustomerOrder;
static OrderRepository()
{
_Orders = new List<Order>();
_CustomerOrder= new List<CustomerOrder>();
_Orders.Add(new Order
{
OrderId = "124",
Product= "Shirts",
Status= "On it's way",
Price= 100.20,
});
_Orders.Add(new Order
{
OrderId= "122",
Product= "Pants",
Status= "Not ready",
Price= 300.30,
});
_Orders.Add(new Order
{
OrderId= "143",
Product= "Deadpool",
Status= "On it's way",
Price= 6.20,
});
_Orders.Add(new Order
{
OrderId= "156",
Product= "Socks",
Status= "Not ready",
Price= 3.30,
});
_CustomerOrder.Add(new CustomerOrder
{
CustomerId = "578",
OrderId = "156",
});
}
public static void SaveNewCustomerOrder(string customerId, string orderId, CustomerOrder customerOrder)
{
order.CustomerID = customerId;
order.OrderId= orderId;
Order.Add(order);
}
控制器:
OrderController.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
{
[HttpPost("customers/{customerId}/orders/{orderId}/overview")]
public IActionResult Post([FromRoute] string customerId, string orderId, [FromBody] CustomerOrder customerOrder)
{
OrderRepository.SaveNewCustomerOrder(customerId, orderId, customerOrder);
return Ok();
}
}
}
【问题讨论】:
-
您的请求正文和网址是什么?
-
@D-Shih URL :
http://localhost:5000/customers/{customerId}/orders/{orderId}/overview请求正文应为customerId and orderId。如果这就是你的要求? -
您可以创建一个无参数 GET 操作,以检查端点是否完全响应。也许这与您的设置有关?
-
不工作是什么意思?你显示代码说它不起作用。什么是预期的行为和实际发生了什么?只有这样,我们才能提供一些帮助,而不是猜测 not working 发生了什么。使用相关详细信息更新问题。
-
@Nkosi 我希望显示新订单并检索已创建的代码 201。
标签: c# .net post asp.net-core