简体   繁体   中英

Optional query strings in ASP.NET Web API?

I'm trying to handle these requests with the same controller action:

  1. localhost:52000/api/messages
  2. localhost:52000/api/messages?page=1
  3. localhost:52000/api/messages?date=2019/29/11&page=1

I made a controller action as following:

[Route("api/messages")]
[HttpGet]
public HttpResponseMessage getMessage(DateTime? date, int? page)

But this only works if the query string value is null, not if the actual query is null.

  • Working: localhost:52000/api/messages?date=&page=

  • Not working (It doesn't find the action): localhost:52000/api/messages

How can I make every api/messages request be handled by the getMessage() action?

Thanks!

try this

[Route("api/messages")]
[HttpGet]
public HttpResponseMessage getMessage(DateTime? date = null, int? page = null)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM