So, you know what HTTP services and the web API are. Here, we are going to develop an application that supports a few various types of requests.
GET /API/students (to get the list of students)
GET /API/students/1 (to get the single student)
POST /API/students (to add the student and add the students' data in
Don't confuse GET and POST data requests, we use get request to get a list of resources or data.
And we use post requests for creating the new one.
Now, for updating the student, we use the PUT application.
PUT /API/customers/1
This way the client id is in the URL and the data or properties to be updated will be in the body of the request. And lastly, remove the student.
Delete /API/customers/1
We send the HttpDelete request to the endpoint. So what you see here, in terms of request types and parameters is a standard convention mentioned for requesting REST (Representational State Transfer)
Step 1
First, create an ASP.NET Web application project in Visual Studio and call it RestAPI. You can do this by selecting File->New->Project->ASP.NET Web Application and clicking OK.
After clicking the OK button, the following window would get displayed from where you need to select the Web API and click the OK button.
Step 2
We will now build the resource classes below to manage our GET, POST, PUT and DELETE services. Right-click the Templates folder in the Project Explorer window and select Add=>Class(see below).
Now it would make it easy for you to write actions to the API.
public IEnumerable GetStudents()
{
}
Since we return a list of items, this action per convention will reply to.
// Get /API/students
So that's the convention embedded in the ASP.Net. Now, in this action, we will use our context to retrieve customers from the database.
namespace RestAPI.Controllers.API
{
public class StudentsController: ApiController
{
private readonly ApplicationDbContext _context;
public StudentsController ()
{
_context = new ApplicationDbContext ();
}
// GET /API/students
public IEnumerable GetStudents()
{
Return _context.Students.ToList();
}
}
}
If the resource is not found, we return the not found HTTP reply otherwise we return the object.
// POST /api/students
[HttpPost]
public Customer CreateStudent(Student student)
{
}
This is the client argument that will be in the body of the request and will be automatically initialized by ASP.NET Web API Framework. Now, we should mark that action with HttpPost as we create the resource. And if we follow the naming convention, we don't even need to place the verb action upon action.
// POST /API/students
public Student PostStudent(Student student )
{
}
Initially, however, this is not a good approach. Suppose you refactor the code in the future and rename your action then presumably your code will break. Therefore, you should always use the HTTP verbs at the top of the action.
Let's now insert the student object into the database with post request of api action.
// POST /api/students
[HttpPost]
public Student CreateStudent(Student student)
{
if (!ModelState.IsValid)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
_context.Students.Add(student);
_context.SaveChanges();
return student;
}
Another measure: assume we want to update the student.
// PUT /api/students/1
[HttpPut]
public void UpdateStudent(int id, Student student)
{
if (!ModelState.IsValid)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
var studentmgr = _context.Studentd.SingleOrDefault(x => x.Id == id);
if (studentmgr == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
studentmgr.Name = student.Name;
studentmgr.Std = student.Std;
_context.SaveChanges();
}
Here in this scenario, different people have different views about making the void or the object.
And if we do the removal action from APIs,
// Delete /API/students/1
[HttpDelete]
public void DeleteStudent(int id)
{
var stdmr = _context.Students.SingleOrDefault(x => x.ID == id);
if (stdmr == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
_context.Students.Remove(stdmr);
// Now the object is labeled as deleted in memory.
// Now it is done
_context.SaveChanges();
}
Here's how we use the relaxing convention to construct the API.