我有以下控制器/动作:
public class SomeThingController
{
IEnumerable<SomeThing> Search(DateTime minDate, DateTime maxDate, bool summaryOnly = true){}
}
我们的想法是不必指定summaryOnly参数,但必须指定minDate和maxDate。
有人可以为上述提供路线吗?
答案 0 :(得分:3)
您可以尝试以下路线:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "Search",
routeTemplate: "api/search/{minDate}/{maxDate}/{summaryOnly}",
defaults: new {
summaryOnly = RouteParameter.Optional,
controller = "SomeThing",
action = "search"
}
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
然后:
public class SomeThingController : ApiController
{
[HttpGet]
public IEnumerable<SomeThing> Search(DateTime minDate, DateTime maxDate, bool summaryOnly = true)
{
...
}
}
然后您可以像这样请求此端点:
/api/search/2013-02-08/2013-02-10/
或:
/api/search/2013-02-08/2013-02-10/false