I have a WebApi project where I have a method that takes a start date and an end date, runs a report, and returns the results. I'm trying to come up with the route for this and so far its looking like
/api/MarketingEmailStatisticsReports/DateRange/{startDate}/{endDate}
Perhaps there is a better route but that really isn't the problem.
I was testing this by simply opening a browser and typing the URL. If I set the startDate and endDates as DateTime fields, I get a 404 Not Found. If I make them strings, I can enter the method but the dates aren't parsable when I attempt to convert them from strings to actual DateTime values.
I realize that if I use "/" characters in the date that will be a problem so I did attempt to use "-" characters as the separator between the month-day-year values.
After looking at some of the Microsoft documentation, I saw how you can put route constraints to check for a DateTime values as well as a regular expression to make sure the value was entered in the correct format. I have taken this advice and am using the following:
[HttpGet]
[ResponseType(typeof(MarketingEmailStatisticsReport))]
[Route("/api/MarketingEmailStatisticsReports/DateRange/{startDate:datetime:regex(^\\d{4}-\\d{2}-\\d{2}$)/{endDate:datetime:regex(^\\d{4}-\\d{2}-\\d{2}$)}")]
public IHttpActionResult Get(DateTime startDate, DateTime endDate)
I get this exception:
ArgumentException: parsing "^\d{4}-\d{2}-\d{2}$)/{endDate:datetime:regex(^\d{4}-\d{2}-\d{2}$" - Too many )'s.
This is the URL I was using:
api/MarketingEmailStatisticsReports/DateRange/2017-05-22/2017-05-23
It doesn't look to me that I have too many ")" characters... What do I need to do to get this to work, or should I pass the dates on the querystring instead?
You forgot to close startDate
parameter definition:
.../DateRange/{startDate:datetime:regex(^\\d{4}-\\d{2}-\\d{2}$)}/{endDate:datetime...
^
this curly bracket