mercredi 9 avril 2014

c# - ASP.NET MVC4 WebAPI : paramètres facultatifs - Stack Overflow


I need to implement the following WebAPI method:


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

All of the parameters can be null, i.e. the caller can specify from 0 to all the 5 parameters.


In MVC4 beta I used to do the following:


public class BooksController : ApiController
{
// GET /api/books?author=tolk&title=lord&isbn=91&somethingelse=ABC&date=1970-01-01
public string GetFindBooks(string author, string title, string isbn, string somethingelse, DateTime? date)
{
// ...
}
}

MVC4 RC doesn't behave like this anymore. If I specify less than 5 parameters, it replies with a 404 saying


No action was found on the controller 'Books' that matches the request.

What is the correct method signature to make it behave like it used to, without having to specify the optional parameter in the URL routing?




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.




Use initial default values for all parameters like below


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



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.




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} _
)


I need to implement the following WebAPI method:


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

All of the parameters can be null, i.e. the caller can specify from 0 to all the 5 parameters.


In MVC4 beta I used to do the following:


public class BooksController : ApiController
{
// GET /api/books?author=tolk&title=lord&isbn=91&somethingelse=ABC&date=1970-01-01
public string GetFindBooks(string author, string title, string isbn, string somethingelse, DateTime? date)
{
// ...
}
}

MVC4 RC doesn't behave like this anymore. If I specify less than 5 parameters, it replies with a 404 saying


No action was found on the controller 'Books' that matches the request.

What is the correct method signature to make it behave like it used to, without having to specify the optional parameter in the URL routing?



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.



Use initial default values for all parameters like below


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


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.



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 commentaires:

Enregistrer un commentaire