What is Web API
ASP.NET Web API is a framework that makes it easy to build
HTTP services that reach a broad range of clients, including browsers and
mobile devices. ASP.NET Web API is an ideal platform for building RESTful
applications on the .NET Framework.
Note: ASP.Net Web API can also be used as a stand-alone Web
services application.
Let us create a simple WebAPI and consume it in another
application. Create a New Project, and follow below steps:
In Models, create a Class (usually Data Model) and name it Customer.
Code
- public class Customer
- {
- public int CustomerId
- {
- get;
- set;
- }
- public string CustomerName
- {
- get;
- set;
- }
- public string Country
- {
- get;
- set;
- }
- }
Add a Controller and name it CustomerController.
Now let us assign Properties to Customer and return that
object.
Code
- using SampleWebAPI.Models;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Text;
- using System.Web.Http;
- using System.Web.Mvc;
-
- namespace SampleWebAPI.Controllers
- {
- public class CustomerController: ApiController
- {
- public Customer GetCustomer()
- {
- Customer customer = new Customer
- {
- CustomerId = 101, CustomerName = "ABC", Country = "India"
- };
- return customer;
- }
- }
- }
When you run the above code you will be able to see a browser opening in that. Just go for that Web API controller like the following:
Here you will be able to see a JSON file downloading. We will consume this in another application and will make use of it. Now let us create a new Console appication and try to access
this service.
Create a New Project and add the following code.
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace CustomerDetails
- {
- class Program
- {
- public class Customer
- {
- public int CustomerId
- {
- get;
- set;
- }
- public string CustomerName
- {
- get;
- set;
- }
- public string Country
- {
- get;
- set;
- }
- }
- static void Main(string[] args)
- {
- var url = "http://localhost:61453/api/Customer/GetCustomer";
- var t = _download_serialized_json_data < Customer > (url);
- Console.WriteLine("Customer Details:");
- Console.WriteLine("Customer ID :" + t.CustomerId.ToString());
- Console.WriteLine("Customer Name:" + t.CustomerName);
- Console.WriteLine("Customer Country:" + t.Country);
- Console.ReadLine();
-
- }
- private static T _download_serialized_json_data < T > (string url) where T: new() {
- using(var w = new System.Net.WebClient())
- {
- var json_data = string.Empty;
-
- try {
- Console.WriteLine("Started downloading data");
- json_data = w.DownloadString(url);
- Console.WriteLine("Completed downloading");
- } catch (Exception) {}
-
- return !string.IsNullOrEmpty(json_data) ? JsonConvert.DeserializeObject < T > (json_data) : new T();
- }
- }
- }
- }
I’ve added Newtonsoft.Json deserializer from Nuget. Now run, WebAPI application since it is not hosted in IIS
now, and in parallel run Console, you should be able to see the data.
Advantages of Web API
It supports convention-based CRUD Actions since it works
with HTTP verbs GET, POST, PUT and DELETE. Can be Hosted in IIS or within application. Response can be Media Formattable to JSON or XML.