首页 > 解决方案 > 在 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();
       }
    }
 }

标签: c#.netpostasp.net-core

解决方案


你应该更新你的 HttpPost("Path")

根据这个这个

最好用

[RoutePrefix("Path")]与类/控制器和

[Route("Path")]动作方法装饰器。


推荐阅读