
I already has discussed that ASP.NET Web API is a framework that simplifies the creation of HTTP services. We can build loosely coupled services as Web API follows REST architecture. Another advantage of using HTTP services is that it can be consumed by a wide range of clients.As we are going to perform CRUD (Create, Read, Update, Delete) operations in this Web Development article using HTTP services, we must understand that these how these operations map to basic HTTP verbs.
- Create -> POST
- Read -> GET
- Update -> PUT
- Delete -> DELETE
- You can follow here to get a comprehensive list of FAQs or ASP.NET Web API Interview Questions. This Web API Tutorial is designed to grasp the core Web API Concepts with few tricky Interview Questions List.
- Building a Real World Application using ASP.NET Core and Angular 2
In order to get start with coding, please create a new ASP.NET MVC 4 Project using Visual Studio and choose Web API template. When the new project is created successfully, you can easily find “Model”, “View” and “Controller” folders inside it.
First of all, we will be creating a new domain model class inside the model folder say “Student.cs” as:
1 2 3 4 5 6 |
public class Student { public string StudentID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } } |
For a better and clean separation, we will add another class “StudentRepository.cs” which will actually perform the CRUD operations. For the purpose of simplicity, I am not going to write complete database interaction code here. You can have implementation of your choice, for example, LINQ or ADO.NET Entity Framework etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class StudentRepository { private static List<Student> students; public static List<Student> GetAllStudents() { //Code logic to get all students. } public static Student GetStudent(string studentID) { //Code Logic to get all students. } public static void RemoveStudent(string studentID) { //Code Logic to delete a student } public static void AddStudent(Student student) { //Code Logic to Add a new student. } public static void UpdateStudent(Student student) { //Code Logic to Update a student. } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class StudentsController : ApiController { public List<Student> Get() { return StudentRepository.GetAllStudents(); } public Student Get(string id) { return StudentRepository.GetStudent(id); } public void Post(Student Student) { StudentRepository.AddStudent(Student); } public void Put(Student Student) { StudentRepository.UpdateStudent(Student); } public void Delete(string id) { StudentRepository.RemoveStudent(id); } } |
In this ASP.NET Web API Tutorial, we have completed the code for performing CRUD operations using ASP.NET Web API. In second part of this article, we will focus on writing the code for consuming the service.
public ActionResult GetEmployeeById()
{
//Code logic here.
return View();
}
What’s true about the above code.
- A. “GetEmployeeById” action method with be identified and called by name “GetEmployeeById”.
- B. “GetEmployeeById” action method with be identified and called by name “GetById”.
- C. Above code will generate an error because of wrong return type.
- D. Action method can’t be called because of duplicate action method names.
For a complete ASP.NET MVC online test and Practice Exams, Click Here.
Correct Answers: B
Other Related Articles:
- WebForms Vs MVC
- ViewBag Vs ViewData Vs TempData in ASP.NET MVC
- Your First ASP.NET MVC 5 application using Entity Framework
- Practical guide to WCF RESTful Service
- Top 5 new features in ASP.NET Web API
- Model Validation in ASP.NET Web API
- What’s new in WCF 4.5
- 3 ways to generate proxy for a WCF Service
Top 10 Interview Questions and Answers Series:
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
Nice post. By the way, I would like to know how you got those code blocks in that grey box. Can you please share the trick? Thanks in advance.
Thanks Braj. In order to gray code box, you need to understand HTML. I modified the HTML.
Your is very nice article about Performing CRUD operations using ASP.NET Web API.
Can you please share the client code to make call to webAPI.
Thanks
Shrirang
Dear Shrirang,
Thanks for your appreciation.
Part 2 of this tutorial is about consuming ASP.NET Web API with client code.
Thanks
Imran
I believe you are making some mistake while calling a method. This type of error occurs normally when someone calls a method at class level instead of making call inside a method.
If unable to resolve, please share the code where you’r getting this error.
hello,
ur explanation is really very good,my code got executed..thanx
hope you will respond to my further queries !!
This error clearly indicates that “Product” is inaccessible in your code. Please make sure that your model “Product” is accessible in controller code? Verify the namespaces if you explicitly have given?
Your tutorial is very nice.
Can you please share the source code,implementing any of the method in ‘StudentRepository’ class.I am confused on how to implement CRUD operation using linq or ADO.NET entity framework
Your tutorial is very nice.
Can you please share the source code,implementing any of the method in ‘StudentRepository’ class.I am confused on how to implement CRUD operation using linq or ADO.NET entity framework
I want to see some articles explaining CRUD in asp .net Web API 2 using angular 2, in the same lucid and beautiful manner as this article is.. so.. which one ..?
This gives a clear basic understanding.
Thanks
Thanks Lizyantoni for your appreciation.
You can follow the link for Practical Guide to ASP.NET Web API with all related concepts and implementations here:
http://www.webdevelopmenthelp.net/2014/02/practical-guide-asp-net-web-api.html
Also, for updated version i.e. ASP.NET Core Web API, you can follow the below Series of tutorials here.
http://www.webdevelopmenthelp.net/2017/09/asp-net-core-web-api-tutorial-part-1.html