0

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) { ... }
}
1

2 Answers 2

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

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) { ... }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.