ServiceStack.Net 的多个可选参数
我正在尝试使用 ServiceStack.Net 实现具有多个可选参数的服务
目前我的路线如下所示,
Routes.Add<SaveWeek>("/save/{Year}/{Week}");
我想支持这样的 uri:
/保存/2010/12/星期一/4/星期二/6/星期三/7
即星期一=4、星期二=6 和星期三=7
但是我希望能够忽略日期,即调用该服务的人可以决定是否想要保存每天的每个值...
即像这样缺少参数值
?星期一=4&星期三=7&星期五=6
当然,一种解决方案是采用以下路线,当我不想保存该值时只需传递 0 即可。
Routes.Add<SaveWeek>("/save/{Year}/{Week}/{Monday}/{Tuesday}}/{Weds}/{Thurs}/{Fri}/{Sat}/{Sun}");
但是......有没有更好的方法来实现这个功能?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您的路线要求开始变得过于复杂时,只需添加通配符路径最终就会变得更容易,以便您可以自己解析查询字符串的其余部分。即在这种情况下,由于查询字符串的第一部分保持不变,您可以添加通配符映射来存储查询字符串的可变部分,即:
ServiceStack 仍将使用 年份 和 < 填充部分 DTO strong>Week 字段(以及在查询字符串中传递的任何字段)。 url 的其余可变部分存储在 DaysString 中,然后您可以手动解析它。因此,上述映射将能够匹配如下网址:
/save/2010/12/Monday/4/Tuesday/6?Wednesday=7
并在请求 DTO 中填充以下变量:
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:
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: