Suggested Videos
Part 8 - Implementing Delete method in ASP.NET Web API
Part 9 - Implementing PUT method in ASP.NET Web API
Part 10 - Custom method names in ASP.NET Web API
(追記) (追記ここまで)
In this video we will discuss using query string parameters in ASP.NET Web API.
(追記) (追記ここまで)
Let us understand query string parameters in ASP.NET Web API with an example.
We want to modify the following Get() method in EmployeesController so that we can retrieve employees by gender
Depending on the value we specify for query string parameter gender, the Get() method should return the data.
If the value for gender is not Male, Female or All, then the service should return status code 400 Bad Request. For example, if we specify ABC as the value for gender, then the service should return status code 400 Bad Request with the following message.
Value for gender must be Male, Female or All. ABC is invalid.
Below is the modified Get() method
ASP.NET Web API tutorial for beginners
Part 8 - Implementing Delete method in ASP.NET Web API
Part 9 - Implementing PUT method in ASP.NET Web API
Part 10 - Custom method names in ASP.NET Web API
(追記) (追記ここまで)
In this video we will discuss using query string parameters in ASP.NET Web API.
(追記) (追記ここまで)
Let us understand query string parameters in ASP.NET Web API with an example.
We want to modify the following Get() method in EmployeesController so that we can retrieve employees by gender
publicclassEmployeesController : ApiController
{
publicIEnumerable<Employee> Get()
{
using (EmployeeDBEntities entities = newEmployeeDBEntities())
{
return entities.Employees.ToList();
}
}
}
Depending on the value we specify for query string parameter gender, the Get() method should return the data.
Query String | Data |
---|---|
http://localhost/api/employees?gender=All | All Employees |
http://localhost/api/employees?gender=Male | Only Male Employees |
http://localhost/api/employees?gender=Female | Only Female Employees |
If the value for gender is not Male, Female or All, then the service should return status code 400 Bad Request. For example, if we specify ABC as the value for gender, then the service should return status code 400 Bad Request with the following message.
Value for gender must be Male, Female or All. ABC is invalid.
Below is the modified Get() method
- Gender is being passed as a parameter to the Get() method
- Default value is "All". The default value makes the parameter optional
- The gender parameter of the Get() method is mapped to the gender parameter sent in the query string
publicHttpResponseMessage
Get(string gender = "All")
{
using (EmployeeDBEntities entities = newEmployeeDBEntities())
{
switch (gender.ToLower())
{
case"all":
return Request.CreateResponse(HttpStatusCode.OK,
entities.Employees.ToList());
case"male":
return Request.CreateResponse(HttpStatusCode.OK,
entities.Employees.Where(e
=> e.Gender.ToLower() == "male").ToList());
case"female":
return Request.CreateResponse(HttpStatusCode.OK,
entities.Employees.Where(e
=> e.Gender.ToLower() == "female").ToList());
default:
return Request.CreateErrorResponse(HttpStatusCode.BadRequest,
"Value for gender must
be Male, Female or All. " + gender + " is invalid.");
}
}
}
ASP.NET Web API tutorial for beginners
5 comments:
Can anyone please help me with this ?:
Reply Deletewhen I am creating the method with return type IHttpActionResult everything is ok, but when I am creating it as above with return type HttpResponseMessage I get the following error :
Method not found: 'System.Net.Http.HttpRequestMessage System.Web.Http.ApiController.get_Request()'.
what can I do to make this work ?
Thank you in advance for your help.
In Visual Studio, Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution... -> Browse -> Search for "Microsoft.AspNet.WebApi.Client"
DeleteInstall it.
how to restrict Parameter Manipulation in update user detail page.
Reply DeleteThis comment has been removed by the author.
Reply DeleteAs we know how to use query in get calls, Can we use query string in post calls as well as ?
Reply DeleteIt would be great if you can help share these free resources
[フレーム]