Asp.Net WebAPI is a framework for building RESTful HTTP services which can be used across a wide range of clients including web, phone and desktop applications.
Introduction
This article shows how to use Web API in ASP.NET web forms. Visual Studio 2013 IDE is used in this article.
To use Web API in a Web Forms application, there are two main steps:
- Add a Web API controller that derives from the ApiController class.
- Add a route table to the Application_Start method.
Lets start creating a Web Forms Project
- Step 1 Create a New Project Open visual studio 2013 -> click on File -> New Project -> Create new ASP.NET Web Application -> Name it as WebAPIforms
Select Web Forms Template and click on OK
- Step 2 Now Lets create Model and Controller.Here i have used the same Model and controller as in the Part 1 article
Right-click the project and select Add Class. Name the class Student, and add the following implementation:public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Standard { get; set; }
public decimal Marks { get; set; }
}
- Step 3 Now Lets add a web API controller
Right-click the project. Select Add New Item.
Under Installed Templates, expand Visual C# and select Web. Then, from the list of templates, select Web API Controller Class(v2). Name the controller "StudentsController
" and click Add.
The Add New Item wizard will create a file named StudentsController.cs. Delete the methods that the wizard included and add the below methods:
The controller defines two methods that return Students
- The GetAllStudents method returns the entire list of products as an IEnumerable<Student> type.
- The GetStudentById method looks up a single student by its ID.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPIforms.Models;
namespace WebAPIforms
{
public class StudentsController
: ApiController
{
Student[] Students = new Student[]
{
new Student { Id = 1, Name = "Sagar", Standard = "seventh", Marks = 60 },
new Student { Id = 2, Name = "Sachin", Standard = "seventh", Marks = 70 },
new Student { Id = 3, Name = "Dravid", Standard = "seventh", Marks = 90 },
new Student { Id = 4, Name = "Ramesh", Standard = "seventh", Marks = 90 },
new Student { Id = 5, Name = "Ganguly", Standard = "seventh", Marks = 90 },
new Student { Id = 6, Name = "Rahul", Standard = "seventh", Marks = 90 },
};
public IEnumerable<Student> GetAllStudents()
{
return Students;
}
public Student GetStudentById(int id)
{
var student = Students.FirstOrDefault((p) => p.Id == id);
if (student == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return student;
}
public IEnumerable<Student> GetStudentsByCategory(string standard)
{
return Students.Where(
(p) => string.Equals(p.Standard, standard,
StringComparison.OrdinalIgnoreCase));
}
}
}
Now we have a working web API.Each method on the controller corresponds to one or more URIs
Conclusion
In this article we have learned how to use WEB API in ASP.NET web forms....
Reference
http://msdn.microsoft.com/ASP-NET-Web-API-Tutorial-c476194