By default, the Http verb is mapped to a method in a
controller that has the same name as the verb or starts with the verb. E.g.
HTTP verb Get is mapped to Get(), GetEmployee() or GetSomething() methods.
If the method does not start with Get or not named Get, then ASP.Net Web API does not know which method in our controller is to be mapped to HTTP verb Get. It is going to fail with an error message “The requested resource does not support http method Get – Status code 405 Method Not Allowed”.
So in order to use custom name for a method we need to decorate the method with HttpGet attribute.
If the method does not start with Get or not named Get, then ASP.Net Web API does not know which method in our controller is to be mapped to HTTP verb Get. It is going to fail with an error message “The requested resource does not support http method Get – Status code 405 Method Not Allowed”.
So in order to use custom name for a method we need to decorate the method with HttpGet attribute.
Use query string parameters in Web API:
For our example, we are considering Get method. Depending
upon the value we specify for the query string parameter Gender, the Get method
should return the data.
FromBody and FromUri:
If parameter is simple type like int, bool, double, etc.
then Web API tried to get the values from the URI.
If parameter is of complex type like Customer, Employee,
etc, then Web API tries to get the value from request body. This is the default
behavior of Web API and called as parameter binding.
To change the default parameter binding process, use
FromBody and FromUri attributes.
Use FromBody attribute to force Web API to read simple types
from request body and use FromUri attribute to for Web API to read complex
types from uri (i.e. from route data or query string).
Pass
query string parameters to Web API:
Depending upon the value we provide for the querystring
parameter, Get() method should return the data.
If invalid value is provided, then the service should return
an error message.
No comments:
Post a Comment