用于特定控制器操作的ASP.NET MVC 4路由
本文关键字:NET MVC 4路 ASP 控制器 操作 用于 | 更新日期: 2023-09-27 17:58:04
我有以下控制器/操作:
public class SomeThingController
{
IEnumerable<SomeThing> Search(DateTime minDate, DateTime maxDate, bool summaryOnly = true){}
}
其想法是不必指定summaryOnly参数,但minDate和maxDate必须为.
有人能提供上述路线吗?
您可以尝试以下路线:
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