Asp.net Web API - A Web API (application programming interface) is an framework, that makes easy to build HTTP requests to GET, PUT, POST and DELETE data for clients. Which include browsers and mobile devices. ASP.NET provides REST full (Representational State Transfer) web applications using .NET Framework.
We will create here REST Apis Using MVC step-by-step.
Step 1: Open Visual Studio.
Step 2: {File} Menu (Alt+f)\{Open}\{Project/Solution} OR Press (Ctrl+Shift+N).
Step 3: From {Installed} Templates\{Visaul C#}\{Web}\{ASP.net Web Application (.NET Framework)}.
Step 4: Choose Name ({MyWebApi}) | Location (default) | Solution name ({MyWebApi}) and press {OK}
Step 5: Select {MVC} from installed Template, Check {MVC} and {Web Api} Checkbox and press {OK}
Step 6: From {Build} Menu (Alt+B) click {Build Solution} or Press (Ctrl+Shift+B).
Step 7: Right Click on {Controllers} Folder under Solution Explorer (Ctrl+Alt+L). Choose {Add}, Choose {Controller}.
Step 8: Select {Web Api2 Controller-Empty} from template and click on {Add}
Step 9: Type {Controller Name} (StudentController).
Step 10: Right Click on {Models} under Solution Explorer. Choose {Add}, Choose Class. Type Class Name Students. Click {Add}
Step 11: Copy Following Code and paste following code for {Students.cs} Class
namespace MyWebApi.Models
{
public class Students
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public string StudentClass { get; set; }
public string StudentRollNo { get; set; }
}
}
Step 11: Right Click on {Models} under Solution Explorer. Choose {Add}, Choose Class.
Type Class Name {StudentServices.cs}. Click {Add}
Step 12: Copy Following Code and paste following code for {StudentServices.cs} Class.
you can use even n-tier architecture with Entity Framework or Ado.net with Database connectivity.
using System.Collections.Generic;
using System.Linq;
namespace MyWebApi.Models
{
public class StudentServices
{
string[] s = new string[] { "Amit", "Arun", "Zannat", "Yash", "Rajni", "Sonu", "Rashi", "Romil", "Arun", "Bhart" };
// Get All Students
public List GetStudentList()
{
List l = new List();
foreach(int i in Enumerable.Range(1,10))
{
l.Add(new Students { StudentId=i, StudentClass="8th", StudentRollNo=i.ToString(), StudentName = s[i-1] });
}
return l;
}
// Find A Student by StudentId
public Students GetStudent(int id) => GetStudentList().Find(x => x.StudentId == id);
// Delete A Student
public List DeleteStudent(int id)
{
List li = GetStudentList();
Students st = li.Find(x => x.StudentId == id);
li.Remove(li.Find(x => x.StudentId == st.StudentId));
return li;
}
// Add New Student
public List AddNewStudent(Students s)
{
List li = GetStudentList();
li.Add(new Students { StudentId = s.StudentId, StudentClass= s.StudentClass, StudentName = s.StudentName , StudentRollNo = s.StudentRollNo });
return li;
}
// Update Old Student
public List UpdateStudent(Students s)
{
List li = GetStudentList();
Students st = li.Find(x => x.StudentId == s.StudentId);
li.Remove(li.Find(x => x.StudentId == st.StudentId));
li.Add(new Students { StudentId = s.StudentId, StudentClass = s.StudentClass, StudentName = s.StudentName, StudentRollNo = s.StudentRollNo });
return li;
}
}
}
Step 13: Copy Following Code and paste following code for {StudentController.cs} Class.
using MyWebApi.Models;
using System.Collections.Generic;
using System.Web.Http;
namespace MyWebApi.Controllers
{
public class StudentController : ApiController
{
StudentServices serv = new StudentServices();
//http://localhost:58708/Student/GetAllStudents
[HttpGet, Route("Student/GetAllStudents")]
public Models.ApiResponse GetAllStudents()
{
ApiResponse res = new ApiResponse();
List sl = serv.GetStudentList();
if (sl.Count > 0)
res.StatusCode = Status.Success;
res.ApiResponseData = sl;
return res;
}
//http://localhost:58708/Student/GetStudent/5
[HttpGet, Route("Student/GetStudent/{id}")]
public Models.ApiResponse GetStudent(int id)
{
ApiResponse res = new ApiResponse();
Students sl = serv.GetStudent(id);
if (sl!=null)
res.StatusCode = Status.Success;
res.ApiResponseData = sl;
return res;
}
//http://localhost:58708/Student/DeleteStudent/5
[HttpDelete, Route("Student/DeleteStudent/{id}")]
public Models.ApiResponse DeleteStudent(int id)
{
ApiResponse res = new ApiResponse();
List sl = serv.DeleteStudent(id);
if (sl != null)
res.StatusCode = Status.Success;
res.ApiResponseData = sl;
return res;
}
//http://localhost:58708/Student/UpdateStudent
[HttpPut, Route("Student/UpdateStudent")]
public Models.ApiResponse UpdateStudent(Students s)
{
ApiResponse res = new ApiResponse();
List sl = serv.UpdateStudent(s);
if (sl != null)
res.StatusCode = Status.Success;
res.ApiResponseData = sl;
return res;
}
//http://localhost:58708/Student/AddNewStudent
[HttpPost, Route("Student/AddNewStudent")]
public Models.ApiResponse AddNewStudent(Students s)
{
ApiResponse res = new ApiResponse();
List sl = serv.AddNewStudent(s);
if (sl != null)
res.StatusCode = Status.Success;
res.ApiResponseData = sl;
return res;
}
/*
{
"StudentId": 1,
"StudentName": "Amit",
"StudentClass": "8th",
"StudentRollNo": "1"
}
*/
}
}
Step 14: Add a new {Class} under {Models} naming {ApiResponse.cs}. Copy and paste following code.
namespace MyWebApi.Models
{
public enum Status
{
Success=1,
Failed=0
}
public class ApiResponse
{
public object ApiResponseData { get; set; }
public Status StatusCode { get; set; }
}
}
Now your web apis are ready to use.
You can test your web apis using postman or firefox REST Client plugins.
Press F5 to run your web application for web api services.
Open Postman or REST Client to test your apis.
To know more about, How to consume or use web api's ? Don't forget to visit -
jQuery Ajax Factory Methods and Services
.
- Now you are able to create restful web api using asp.net and c#. If you have any Query or Question or Suggestion don't forget to leave a comment.
If you have any query or question or topic on which, we might have to write an article for your interest or any kind of suggestion regarding this post, Just feel free to write us, by hit add comment button below or contact via Contact Us form.
Your feedback and suggestions will be highly appreciated. Also try to leave comments from your valid verified email account, so that we can respond you quickly.