ServiceStack.Net 的多个可选参数

发布于 2024-12-10 02:37:16 字数 598 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一江春梦 2024-12-17 02:37:16

当您的路线要求开始变得过于复杂时,只需添加通配符路径最终就会变得更容易,以便您可以自己解析查询字符串的其余部分。即在这种情况下,由于查询字符串的第一部分保持不变,您可以添加通配符映射来存储查询字符串的可变部分,即:

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

ServiceStack 仍将使用 年份 和 < 填充部分 DTO strong>Week 字段(以及在查询字符串中传递的任何字段)。 url 的其余可变部分存储在 DaysString 中,然后您可以手动解析它。因此,上述映射将能够匹配如下网址:

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

并在请求 DTO 中填充以下变量:

  • Year: 2010
  • Week: 12
  • Wednesday: 7
  • DaysString :周一/4/周二/6

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文