Reputation: 791
If I have an endpoint
public class OrdersController : ApiController
{
[Route("customers/{customerId}/orders")]
[HttpPatch]
public IEnumerable<Order> UpdateOrdersByCustomer(int customerId) { ... }
}
I can make the calls like this:
http://localhost/customers/1/orders
http://localhost/customers/bob/orders
http://localhost/customers/1234-5678/orders
But what if I want to send a date as part of the query string?
For example I want to send the following: http://localhost/customers/1234-5678/orders?01-15-2019
How can I set my endpoint?
public class OrdersController : ApiController
{
[Route("customers/{customerId}/orders")]
[HttpPatch]
public IEnumerable<Order> UpdateOrdersByCustomer(int customerId, DateTime? effectiveDate) { ... }
}
Upvotes: 0
Views: 120
Reputation: 306
You could modify your route attribute to the following:
public class OrdersController : ApiController
{
[Route("customers/{customerId}/orders/{effectiveDate?}")]
[HttpPost]
public IEnumerable<Order> UpdateOrdersByCustomer(int customerId, DateTime? effectiveDate) { ... }
}
Upvotes: 0
Reputation: 1559
In a [HttpPatch]
type of request, only primitive types are can be used as query strings. And DateTime
is not a primitive type.
As your example suggests that you need to pass just the date
part to the query string, therefore, you can use string
datatype instead and convert it to date inside the action method. Something like this:
public IEnumerable<Order> UpdateOrdersByCustomer(int customerId, string effectiveDate) //changed datatype of effectiveDate to string
{
//converting string to DateTime? type
DateTime? effDate = string.IsNullOrEmpty(effectiveDate) ? default(DateTime?) : DateTime.Parse(str);
// do some logic with date obtained above
}
Upvotes: 1