我正在尝试创建新的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();
}
}
}
答案 0 :(得分:0)
[HttpPost("customers/{customerId}/orders/{orderId}/overview")]
public IActionResult Post([FromRoute] string customerId, [FromRoute] string orderId, [FromBody] CustomerOrder customerOrder)
{
OrderRepository.SaveNewCustomerOrder(customerId, orderId, customerOrder);
return Ok();
}
我不认为您想要的软件是什么,但是像[fromRoute]参数一样开始添加orderID,在进行调试时,您是否在参数中看到正确的值?您正在使用aspnet .core 2.0吗?
您确定路径正确吗?我在之前的评论中读过这一条: http://localhost:5000/customers/ {customerId} / orders / {orderId} /概述
检查是否是此路径: http://localhost:5000/api/customers/ {customerId} / orders / {orderId} /概述
答案 1 :(得分:0)
答案 2 :(得分:0)
我已经复制了您的代码,但是您的期望有一些问题。
1)
public static void SaveNewCustomerOrder(string customerId, string orderId, CustomerOrder customerOrder)
{
order.CustomerID = customerId;
order.OrderId= orderId;
Order.Add(order);
}
在该方法内,您尝试添加customerId和orderId,但它们不属于订单类。然后,您尝试将此订单添加到订单列表中。 我认为您可能打算传递订单而不是客户订单。我有以下类和控制器在工作:
控制器:
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using ben22.Repositories;
using ben22.Models.Models;
namespace ben22.Controllers
{
public class OrderController : Controller
{
[HttpPost("customers/{customerId}/orders/{orderId}/overview")]
public IActionResult Post([FromRoute] string customerId, string orderId, [FromBody] Order order)
{
OrderRepository.SaveNewCustomerOrder(customerId, orderId, new Order());
return Ok();
}
}
}
更新了SaveNewCustomerOrder:
public static void SaveNewCustomerOrder(string customerId, string orderId, Order order)
{
var newCustomer = new CustomerOrder
{
CustomerId = customerId,
OrderId = orderId
};
_CustomerOrder.Add(newCustomer);
_Orders.Add(order);
}
命中以下端点(替换yourPort):
http://localhost:{yourPort} / customers / 1 / orders / 2 / overview
身体:
{
"orderId": "55",
"product": "Football",
"status": "Approved",
"price": 9.99
}
此外,如果这不起作用,我将在Visual Studio中检查您的launchUrl并可能重新启动它。
答案 3 :(得分:0)
假设以下
我希望显示新订单并检索创建的代码201
以下内容仅是上述请求的演示示例,不能直接从您的示例中获取。
public class OrderController : Controller {
//...removed for brevity
//POST "customers/578/orders/1576/overview
[HttpPost("customers/{customerId}/orders/{orderId}/overview")]
public IActionResult Post(string customerId, string orderId) {
var order = orderRepository.CreateNewCustomerOrder(customerId, orderId);
if(order == null) {
return BadRequest();
}
//return 201 created status code along with
//the route name, route value, and the actual object that is created
return CreatedAtRoute("GetOrder", new { orderId = order.OrderId }, order);
}
//GET orders/157
[HttpGet("orders/{orderId}", Name = "GetOrder")]
public IActionResult GetOrder(string orderId) {
var order = orderRepository.GetOrder(orderId);
if(order == null) {
return NotFound();
}
return Ok(order);
}
}