Optional query string parameters in ASP.NET Web API

后端 未结 5 1156
粉色の甜心
粉色の甜心 2020-12-07 08:57

I need to implement the following WebAPI method:

/api/books?author=XXX&title=XXX&isbn=XXX&somethingelse=XXX&date=XXX

All of

相关标签:
5条回答
  • 2020-12-07 09:01

    It's possible to pass multiple parameters as a single model as vijay suggested. This works for GET when you use the FromUri parameter attribute. This tells WebAPI to fill the model from the query parameters.

    The result is a cleaner controller action with just a single parameter. For more information see: http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api

    public class BooksController : ApiController
      {
        // GET /api/books?author=tolk&title=lord&isbn=91&somethingelse=ABC&date=1970-01-01
        public string GetFindBooks([FromUri]BookQuery query)
        {
          // ...
        }
      }
    
      public class BookQuery
      {
        public string Author { get; set; }
        public string Title { get; set; }
        public string ISBN { get; set; }
        public string SomethingElse { get; set; }
        public DateTime? Date { get; set; }
      }
    

    It even supports multiple parameters, as long as the properties don't conflict.

    // GET /api/books?author=tolk&title=lord&isbn=91&somethingelse=ABC&date=1970-01-01
    public string GetFindBooks([FromUri]BookQuery query, [FromUri]Paging paging)
    {
      // ...
    }
    
    public class Paging
    {
      public string Sort { get; set; }
      public int Skip { get; set; }
      public int Take { get; set; }
    }
    

    Update:
    In order to ensure the values are optional make sure to use reference types or nullables (ex. int?) for the models properties.

    0 讨论(0)
  • 2020-12-07 09:10

    Use initial default values for all parameters like below

    public string GetFindBooks(string author="", string title="", string isbn="", string  somethingelse="", DateTime? date= null) 
    {
        // ...
    }
    
    0 讨论(0)
  • 2020-12-07 09:18

    if you want to pass multiple parameters then you can create model instead of passing multiple parameters.

    in case you dont want to pass any parameter then you can skip as well in it, and your code will look neat and clean.

    0 讨论(0)
  • 2020-12-07 09:23

    Default values cannot be supplied for parameters that are not declared 'optional'

     Function GetFindBooks(id As Integer, ByVal pid As Integer, Optional sort As String = "DESC", Optional limit As Integer = 99)
    

    In your WebApiConfig

     config.Routes.MapHttpRoute( _
              name:="books", _
              routeTemplate:="api/{controller}/{action}/{id}/{pid}/{sort}/{limit}", _
              defaults:=New With {.id = RouteParameter.Optional, .pid = RouteParameter.Optional, .sort = UrlParameter.Optional, .limit = UrlParameter.Optional} _
          )
    
    0 讨论(0)
  • 2020-12-07 09:25

    This issue has been fixed in the regular release of MVC4. Now you can do:

    public string GetFindBooks(string author="", string title="", string isbn="", string  somethingelse="", DateTime? date= null) 
    {
        // ...
    }
    

    and everything will work out of the box.

    0 讨论(0)
提交回复
热议问题