dfe*_*aro 8 c# asp.net asp.net-web-api
我有一个这样的课:
public class FooController : ApiController
{
[System.Web.Http.Route("live/topperformers")]
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public List<string> GetTopPerformers()
{
return new List<string>();
}
}
Run Code Online (Sandbox Code Playgroud)
当我通过访问" http://foo.com/live/topperformers " 访问它时,效果很好.所以现在我想为这个方法添加一个可选的DateTime参数,所以我修改了方法以获取DAteTime参数,并使其可以为空.
public class FooController : ApiController
{
[System.Web.Http.Route("live/topperformers/{dateTime:DateTime}")]
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public List<string> GetTopPerformers(DateTime? dateTime)
{
return new List<string>();
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试访问没有参数的URL时,就像我之前访问的那样 - 它给出了404.在日期值中像"喜欢"一样http://foo.com/live/topperformers/2010-01-01 "工作正常.但没有约会,它给出了404.
我认为Web API以这种方式支持可选参数?我可以简单地重载并拥有两个版本,但这只能通过一种方法实现吗?
sjk*_*jkm 18
设置可选参数= null.试试这个:
public class FooController : ApiController
{
[System.Web.Http.Route("live/topperformers/{dateTime:DateTime?}")]
[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public List<string> GetTopPerformers(DateTime? dateTime = null)
{
return new List<string>();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6399 次 |
| 最近记录: |