C# ASP.NET Web API中的可选查询字符串?
我正在尝试使用相同的控制器操作处理这些请求:C# ASP.NET Web API中的可选查询字符串?,c#,asp.net-web-api2,C#,Asp.net Web Api2,我正在尝试使用相同的控制器操作处理这些请求: localhost:52000/api/messages localhost:52000/api/messages?page=1 localhost:52000/api/messages?日期=2019/29/11&page=1 我执行了如下控制器操作: [Route("api/messages")] [HttpGet] public HttpResponseMessage getMessage(DateTime? date, int? page)
localhost:52000/api/messages
localhost:52000/api/messages?page=1
localhost:52000/api/messages?日期=2019/29/11&page=1
[Route("api/messages")]
[HttpGet]
public HttpResponseMessage getMessage(DateTime? date, int? page)
但这仅在查询字符串值为null时有效,而在实际查询为null时无效
- 工作:
localhost:52000/api/messages?日期=&page=
- 不工作(未找到操作):
localhost:52000/api/messages
api/消息请求都由
getMessage()
操作
谢谢 试试这个
[Route("api/messages")]
[HttpGet]
public HttpResponseMessage getMessage(DateTime? date = null, int? page = null)
为这两个参数设置默认值