与ServiceStack.Net多个可选参数 [英] Multiple Optional Parameters with ServiceStack.Net
问题描述
我试图实现与多个可选参数服务使用ServiceStack.Net
目前我的路线是这样的
Routes.Add< SaveWeek>(/保存/ {年} / {}周);
我要支持这样的URI:
/保存/ 2010/12 /星期一/ 4 /日/ 6 /日/ 7
块引用>即星期一= 4,周二= 6,周三= 7
不过,我想忽略的能力,即天调用该服务可以决定他们是否希望保存每天每个值的人...
即。像这样与缺少的参数值
?周一= 4和周三= 7安培;周五= 6
块引用>当然,一个解决方案将有以下的路线,只传递0的时候,我不希望保存的值。
<$p$p><$c$c>Routes.Add<SaveWeek>(\"/save/{Year}/{Week}/{Monday}/{Tuesday}}/{Weds}/{Thurs}/{Fri}/{Sat}/{Sun}\");
但是.....有没有实现这一功能的更好的办法?
解决方案当您的路由需求开始变得太复杂了它最终将变得更容易只是增加一个通配符路径,以便可以解析查询字符串自己的休息。即因为在查询字符串的第一部分,这种情况下保持不变,你可以添加一个通配符映射存储查询字符串的可变部分,即:
Routes.Add(/保存/ {年} / {周} / {DaysString *});
ServiceStack仍将填充部分DTO与年和星期字段(以及任何查询字符串被传递的字段)。 URL的其余部分变量存储在 DaysString ,你就可以自由手动解析自己。所以上面的映射就能像匹配的URL:
/保存/ 2010/12 /星期一/ 4 /日/ 6?周三= 7
而在你的请求DTO填入以下变数:
- 年份:2010
- 周刊:12
- 星期三:7
- DaysString:周一/ 4 /日/ 6
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/Wednesday/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
这篇关于与ServiceStack.Net多个可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!