So, let’s flip to Visual Studio. In our code, we will add a method for DELETE like this.

This is our class for delete as the return type is void, and we are passing id to which we are going to delete the details of those employees. Then, we created an instance of our TESTEntities. Then, we have to specify the entity we want to remove. So, the lambda is going to return the employee which we want to delete and it removes it from the employees’ collection. And then, you are saving changes.
Now, run the app and open fiddler. We will monitor the request.
At the moment. we got back seven records:

In fiddler, let's change the HTTP verb to DELETE and also pass the id as 7. Let's execute the request.

After you execute that request, go back to SQL Server and check the records.

As you can see from the above record, the id 7 is deleted. Now, let’s look at the response we have got from fiddler.

Notice that it's showing "204 : No content". That is because the return type is void. When the deletion is successful, we should be ideally returning the status code "200: OK", not the 204 error. This is our first problem. The second problem is - when we try to delete an id which does not exist, we do get an exception in fiddler as -

If you look at the response, the status code is,

It's "500 Internal Server Error". So, basically, it's saying there is an internal server error. We do get this status code because of an exception:

This lambda is actually searching for an employee which we don’t have here. So, it returns NULL which is then passed to the remove method and remove method is throwing an exception. If you want to know the exception, click on the JSON tab. You will see the following.

The Exception type is system.argumentNullException. The Value cannot be NULL, which is an exception message. As a result of this exception, we are getting the status code as "500: Internal Server Error".
When we are trying to delete an item which does not exist, we should be returning "404 Not Found " instead of "500 Internal Server Error". So, let’s see how to solve these problems
We will change the return type from void to HttpResponseMessage.

Now, we will write an if condition - If the request is null, it should give a message as employee id does not exist and when it exists, it should delete that request.

So, in our try catch block, we have created a variable called as entity and passed our lambda expression to that variable. And, we have specified our condition respectively. So, our final code for DELETE method is,
At the moment. we got back seven records:

In fiddler, let's change the HTTP verb to DELETE and also pass the id as 7. Let's execute the request.

After you execute that request, go back to SQL Server and check the records.

As you can see from the above record, the id 7 is deleted. Now, let’s look at the response we have got from fiddler.

Notice that it's showing "204 : No content". That is because the return type is void. When the deletion is successful, we should be ideally returning the status code "200: OK", not the 204 error. This is our first problem. The second problem is - when we try to delete an id which does not exist, we do get an exception in fiddler as -

If you look at the response, the status code is,

It's "500 Internal Server Error". So, basically, it's saying there is an internal server error. We do get this status code because of an exception:

This lambda is actually searching for an employee which we don’t have here. So, it returns NULL which is then passed to the remove method and remove method is throwing an exception. If you want to know the exception, click on the JSON tab. You will see the following.

The Exception type is system.argumentNullException. The Value cannot be NULL, which is an exception message. As a result of this exception, we are getting the status code as "500: Internal Server Error".
When we are trying to delete an item which does not exist, we should be returning "404 Not Found " instead of "500 Internal Server Error". So, let’s see how to solve these problems
We will change the return type from void to HttpResponseMessage.

Now, we will write an if condition - If the request is null, it should give a message as employee id does not exist and when it exists, it should delete that request.

So, in our try catch block, we have created a variable called as entity and passed our lambda expression to that variable. And, we have specified our condition respectively. So, our final code for DELETE method is,







Vignesh ManiPosted Sep 27, 2016, 7:04 AM
Nice
Humayun Kabir MamunPosted Sep 27, 2016, 2:27 AM
Nice share...
Bhavik PatelPosted Sep 26, 2016, 11:07 PM
Nice
Upendra Pratap ShahiPosted Sep 26, 2016, 1:57 PM
Nice one..