c# - 在 POST 请求中出现 HTTP 404 错误 - C#
问题描述
我正在尝试创建新的 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);
}
控制器:
订单控制器.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();
}
}
}
解决方案
推荐阅读
- java - 无法获取捕获的图像 uri
- python - 30 天 LeetCode 四月挑战赛第 1 天 - 单号
- youtube-api - 如何通过 YouTube API 获取订阅收入数据?
- node.js - Mongoose.connection.on('error', error => ..} TypeError:Cannot read property 'on' of undefined
- d3.js - Dc.js:无法使用排序命令我的条形图
- graphql - Gatsby develop failed: Fetching SilverStripe data failed Cannot read property 'data' of undefined after setting up silverstripe-gatsby
- shell - Change specific column value in a data file
- scala - Scala / Dotty - 将特征混合到现有对象中
- ionic-framework - 什么是离子 X?
- asp.net-mvc - 如何返回json响应?