ServiceStack : routes and parameters

2019-04-15 22:57发布

问题:

Just discovered ServiceStack last months and i really enjoy working with this great framework. Was reaaly fed up with WCF settings and static method prototyping !

I have a question !

I have created a class : Events that allows to display one or a List of Events using following Routes :

[Route("/events")]
[Route("/events/{Id}")]
public class Event
{
    public ushort Id { get; set; }   
    public string FromDate { get; set; }
    public string ToDate { get; set; }
}

But i would also like to list events between 2 dates using kind of same routes. Actually the route only takes Id as parameter but i wanted to add another route specifying FromDate : I thought something like this would work : /events/01-01-2012/10-01-2012 in order to get all events between 2 dates but it does not work.

Is it possible to define routes that allows to define different routes based on different parameters (taken from DTO) ?

I am newbie with managing Routes and would really like to know how it's possible to define different way of filtering based on different parameters.

Thanks for the help ! Awesome work Demis !

回答1:

How about [Route("/events/from/{FromDate}/to/{ToDate}")] ?

Though my current way of thinking would split them into two requests

[Route("/events/{Id}")]
public class GetEventById
{
    public ushort Id { get; set; }
}

[Route("/events/between/{FromDate}/{ToDate}")]
public class GetEventsBetweenDates
{   
    public string FromDate { get; set; }
    public string ToDate { get; set; }
}


回答2:

On my OnGet() function i want to determine wether a parameter is set to NULL or not. When i added a '?' aside the request data type i got a 'bad request' exception when trying to access my service.

Does that mean that all REQUEST parameters are null for string, 0 for numbers, 01-01-000 for dates etc and that's the value i should test on my OnGet to determine input Querystring parameters ?