我正在尝试发布数据,但是我似乎不太了解这个想法。请按照说明从Google和Stack Overflow来源发布数据,但不走运。
对此还是陌生的,希望我认识现实生活中的人们,逐步解释如何做。非常感谢您的帮助。
这是我到目前为止所拥有的:
型号:
Customer.cs:
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; }
}
}
Order.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; }
public string CustomerId { 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;
static OrderRepository()
{
ORDERS= new List<Order>();
ORDERS.Add(new Order
{
CustomerId = "123",
OrderId = "124",
Product= "Shirts",
Status= "On it's way",
Price= 100.20,
});
ORDERS.Add(new Order
{
CustomerId = "123",
OrderId= "122",
Product= "Pants",
Status= "Not ready",
Price= 300.30,
});
ORDERS.Add(new Order
{
CustomerId = "789",
OrderId= "143",
Product= "Deadpool",
Status= "On it's way",
Price= 6.20,
});
ORDERS.Add(new Order
{
CustomerId = "578",
OrderId= "156",
Product= "Socks",
Status= "Not ready",
Price= 3.30,
});
// Not sure if this POST method should be placed here
using (var client = new WebClient())
{
var values = new NameValueCollection();
values["CustomerId"] = "578";
values["OrderId"] = "156";
var response = client.UploadValues("http://localhost:5000/customers/{CustomerID}/orders/{OrderId}/overview", values);
var responseString = Encoding.Default.GetString(response);
}
}
public static Order GetByOrderid(string customerId, string orderId)
{
return ORDERS
.Where(x => x.CustomerId == customerId)
.Where(x => x.OrderId== orderId)
.SingleOrDefault();
}
public static List<Order> GetAllOrders(string customerId)
{
return ORDERS.Where(x => x.CustomerId == customerId).ToList();
}
}
}
控制器:
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
{
[HttpGet("customers/{id}/orders/{orderId}")]
public Order FindOneOrder(string customerId, string orderId)
{
return OrderRepository.GetByOrderid(customerId, orderId);
}
[HttpGet("customers/{id}/orders")]
public List<Order> GetCustomerOrders(string customerId)
{
return OrderRepository.GetAllOrders(customerId);
}
}
}
更新OrderController:
[HttpPost("customers/{customerId}/orders/{orderId}/overview")]
public async Task<IAsyncResult> Post([FromRoute] string customerId, string orderId, string status, [FromBody] Order order)
{
OrderRepository.SaveNewOrder(customerId, caseId, status, order);
//all that is left is to return something, based on what you need.
//a good return for this method is HTTP 201 - Created
return CreatedAtAction(nameof(FindOneOrder), new { customerId = customerId, orderId = orderId}, order);
}
更新OrderRepository:
public static void SaveNewOrder(string customerId, string orderId, string status, Order order)
{
order.CustomerID = customerId;
order.OrderId= orderId;
order.Status= status;
Order.Add(order);
//here you should also call any web service that does the saving
//or store to the database, depending on your business logic
}
答案 0 :(得分:0)
在您的OrderRepository
中,您首先需要添加一种存储新订单的方法。
public static void SaveNewOrder(string customerId, Order order)
{
order.CustomerId = customerId;
ORDERS.Add(order);
//here you should also call any web service that does the saving
//or store to the database, depending on your business logic
}
然后,在您的OrderController
中,添加另一种方法:
[HttpPost("customers/{id}/orders")]
public async Task<IAsyncResult> Post([FromRoute] string id, [FromBody] Order order)
{
OrderRepository.SaveNewOrder(id, order);
//all that is left is to return something, based on what you need.
//a good return for this method is HTTP 201 - Created
return CreatedAtAction(nameof(FindOneOrder), new { id = customerId, orderId = order.OrderId}, order);
}
我建议您将[HttpPost]
用于新订单,并将[HttpPut]
用于更新现有订单:
[HttpPut("customers/{id}/orders/{orderId}")]
public async Task<IAsyncResult> Put([FromRoute] string id, [FromRoute] string orderId, [FromBody] Order order)
{
//do some work, like updating the order with the id {orderId}
//for the customer with the {id} in the URL.
}
此外,您的FindOneOrder
和GetCustomerOrders
在路由中的参数与原型中定义的参数不同。确保在两个地方都使用{id}
或{customerId}
。