|
In this article I will talk about creating Web API of ASP.NET MVC 4. This will be simple Web API which will return complete list of customers or customer details based on customerid or customename.
You need to Install ASP.NET MVC 4 if you don't have.
Let's write code:
Step 1: Create ASP.NET MVC 4 WebApplication.

Step 2: Select Web API template.

Step 3: The solution explorer will look like below.

Step 4: Add a Customer class in the Model folder and place below code.
using System; using System.Collections.Generic; using System.Linq; using System.Web;
namespace HelloWebAPI.Models { public class Customer { public int CustomerId { get; set; } public string CustomerName { get; set; } public string Address { get; set; } } }
Model represents data, model get serialized to JSON, XML by ASP.NET Web API which is then written in HTTP response body. We will check response body when we complete this example.
Step 5: Expand Controllers folder and you will notice HomeController.cs and ValuesController.cs. Controller object handles HTTP request.
Delete ValuesController.cs and add CustomerController.cs. Select Empty API Controller from Template.

Step 6: Add below code in the CustomerController.cs.
using System; using System.Collections.Generic; using System.Linq; using HelloWebAPI.Models; using System.Net; using System.Net.Http; using System.Web.Http;
namespace HelloWebAPI.Controllers { public class CustomerController : ApiController { Customer[] customers = new Customer[] { new Customer { CustomerId = 100, CustomerName = "Hemant", Address = "Bangalore" }, new Customer { CustomerId = 200, CustomerName = "John", Address = "New York"}, new Customer { CustomerId = 300, CustomerName = "Bob", Address = "Texas" } };
public IEnumerable<Customer> GetAllCustomers() { return customers; }
public Customer GetCustomerById(int customerId) { var customer = customers.FirstOrDefault((p) => p.CustomerId == customerId); if (customer == null) { var resp = new HttpResponseMessage(HttpStatusCode.NotFound); throw new HttpResponseException(resp); } return customer; }
public IEnumerable<Customer> GetCustomerByCategory(string customerName) { return customers.Where((p) => string.Equals(p.CustomerName, customerName,StringComparison.OrdinalIgnoreCase)); } } }
Step 7: Now run the application and type http://localhost:1712/api/customer (Portnumber might be different for you). You will be prompted to save customer like shown below.

Step 8: Now open the file and you will get JSON.
[{"CustomerId":100,"CustomerName":"Hemant","Address":"Bangalore"},{"CustomerId":200,"CustomerName":"John","Address":"New York"},{"CustomerId":300,"CustomerName":"Bob","Address":"Texas"}]
Step 9: Now to search using CustomerId or CustomerName, type below urls.
To retrieve by customerid http://localhost:1712/api/customer/?customerid=200
To retrieve by customername http://localhost:1712/api/customer/?customername=John
Observations
1. Mozilla and Chrome will display the result as XML in the brower.

2. Model object passed as JSON in Response Body. Turn on IE 9 developer tool bar and start Network capturing to see JSON in Response Body.

This ends Hello Web API article.
|