开发者

Multiple Optional Parameters with ServiceStack.Net

I'm trying to implement a service with Multiple Optional Parameters using ServiceStack.Net

At the moment my route looks like this

Routes.Add<SaveWeek>("/save/{Year}/{Week}");

I want to support uris like this:

/save/2010/12/Monday/4/Tuesday/6/We开发者_StackOverflow社区dnesday/7

ie Monday=4, Tuesday=6 and Wednesday=7

However I want the ability to ignore days i.e. the person calling the service can decide if they want to save each value for each day...

i.e. Like this with missing parameter values

?Monday=4&Wednesday=7&Friday=6

Of course one solution would be to have the following route and just pass 0 when I don't want to save the value.

Routes.Add<SaveWeek>("/save/{Year}/{Week}/{Monday}/{Tuesday}}/{Weds}/{Thurs}/{Fri}/{Sat}/{Sun}");

But..... is there a better way of achieving this functionality?


When your Route requirements start to get too complicated it will eventually become easier just to add a wild card path so you can parse the rest of the querystring yourself. i.e. in this case since the first part of the querystring remains constant you can add a wild card mapping to store the variable parts of the querystring, i.e:

Routes.Add("/save/{Year}/{Week}/{DaysString*}");

ServiceStack will still populate the partial DTO with the Year and Week fields (as well any fields that were passed in the querystring). The remaining variable parts of the url is stored in the DaysString which you are then free to parse yourself manually. So the above mapping will be able to match urls like:

/save/2010/12/Monday/4/Tuesday/6?Wednesday=7

And populate the following variables in your Request DTO:

  • Year: 2010
  • Week: 12
  • Wednesday: 7
  • DaysString: Monday/4/Tuesday/6
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜