Introduction to ASP.NET Web API “ASP.NET Web API is a framework that simplifies the creation of HTTP services“ Using ASP.NET Web API we can create HTTP services those are non-SOAP based like plain XML or JSON strings etc. with added advantages. Allowing to create resource-oriented services using the full features of HTTP.

Exposing services to a variety of clients easily like browsers or mobile devices etc. ASP.NET Web API Tutorial, I would like to clarify one misconception that ASP.NET Web API has replaced WCF. WCF is still a powerful programming model for creating SOAP based services that use a variety of transport protocols like HTTP, TCP, Named Pipes or MSMQ etc. You can find the same implementation by using WCF REST in another article i.e. “ Before diving into details while going through this, I would like to clarify one misconception that ASP.NET Web API has replaced WCF. WCF is still a powerful programming model for creating SOAP based services that use a variety of transport protocols like HTTP, TCP, Named Pipes or MSMQ etc. You can find the same implementation by using WCF REST in another article i.e. “ 5 simple steps to create your first RESTful service “. Learn the Foundation: HTTP & REST Concepts Resources and URI HTTP Methods HTTP Status Codes HTTP Content Internet Media Types REST JSON and XML

Creating ASP.NET Web API service that supports CRUD (Create, Retrieve, Update, Delete) operations

Building a Real World Application using ASP.NET Core and Angular 2 Apart from Visual Studio 2010 or 2012, we also need MVC 4.0 to implement this HTTP service. For the purpose of this implementation, I am going to use Visual Studio 2010.

You can download MVC 4.0 for Visual Studio 2010 from Microsoft as follows:

Following are 3 simple steps to create HTTP service that returns non-SOAP based data. Create Web API Project

Prepare domain Model

Adding Controller class

Test your ASP.NET Web API skill by answering a simple Question. A. GET

B. POST

C. DELETE

D. PUT To further test your ASP.NET Web API skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here. You are designing an ASP.NET Web API application. You need to select an HTTP verb to allow blog administrators to moderate a comment. Which HTTP verb should you use? Correct Answer: D



Let’s move forward step by step to create a simple HTTP service using ASP.NET Web API.

ASP.NET Web API Tutorial with Simplified Approach 1. Create Web API Project Open Visual Studio and create “New Project” i.e. File -> New Project.

Choose “ASP.NET MVC 4 Web Application” template and name project as “FirstWebAPIService”.

When you click “OK” button, a new window will appear for selecting a sub-template. Actually for ASP.NET MVC 4 Web Application, we have multiple sub-options i.e. Empty, Internet Application, Web API etc.

Choose “Web API” and simply press “OK” button.

A default ASP.NET MVC 4 Web API template project is created. As its an MVC application template, so you will easily find “Model”, “View” and “Controller” folders inside it. 2. Preparing domain Model Now in a second step in this asp.net web api tutorial, we need to prepare the model. Right click on the “Model” folder and choose “Class” under “Add” from the context menu as shown in figure.

Name the class as “Product.cs”.

Here is the code for Product class.

public class Product { public int ProductID { get; set; } public string ProductName { get; set; } public string ProductCategory { get; set; } public int Price { get; set; } } 1 2 3 4 5 6 7 public class Product { public int ProductID { get ; set ; } public string ProductName { get ; set ; } public string ProductCategory { get ; set ; } public int Price { get ; set ; } }

Right click on the “Controller” folder and choose “Controller” under “Add” from the context menu as shown in figure.

Name the controller as “ProductsController”.

public class ProductsController : ApiController { Product[] products = new Product[] { new Product { ProductID = 1, ProductName = "Product 1", ProductCategory= "Category 1", Price = 120 }, new Product { ProductID = 2, ProductName = "Product 2", ProductCategory= "Category 1", Price = 100 }, new Product { ProductID = 3, ProductName = "Product 3", ProductCategory= "Category 2", Price = 150 }, new Product { ProductID = 4, ProductName = "Product 4", ProductCategory= "Category 3", Price = 90 } }; public IEnumerable<Product> GetProducts() { return products; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public class ProductsController : ApiController { Product [ ] products = new Product [ ] { new Product { ProductID = 1 , ProductName = "Product 1" , ProductCategory = "Category 1" , Price = 120 } , new Product { ProductID = 2 , ProductName = "Product 2" , ProductCategory = "Category 1" , Price = 100 } , new Product { ProductID = 3 , ProductName = "Product 3" , ProductCategory = "Category 2" , Price = 150 } , new Product { ProductID = 4 , ProductName = "Product 4" , ProductCategory = "Category 3" , Price = 90 } } ; public IEnumerable < Product > GetProducts ( ) { return products ; } }

Don’t forget to add “using FirstWebAPIService.Models;” at the top of the controller class.

Controller class plays an important role, because request coming from client hits the controller first. Then the controller decides which model to use to serve the incoming request. So, in order to add a controller:

Now, it’s time to test your HTTP service using ASP.NET MVC Web API.

Run the application by pressing “CTRL+F5”, Welcome window will appear as follows:

In order to call our Product controller, change the URL as “http://localhost:XXXX/api/products”. You will see the results as shown in following output window.

Final output returned can be displayed differently by different browsers. Here is the output of google chrome version 29.0.1547.66.

Answer ASP.NET Web API Question to verify your skill. A. Use ViewBag.

B. Use Partial View against Web API method.

C. You can’t return view from ASP.NET Web API method.

D. None of Above. To further test your ASP.NET Web API skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here. You are working as a Web API developer with WebDevTutorials. You have developed a Student HTTP Service using ASP.NET Web API technology and you want to return a View from Web API method. Correct Answer: C



Hopefully, this simple web api tutorial will be helpful for developers to code their first HTTP service using ASP.NET MVC Web API. Further, for performing all CRUD operations using Web API, Follow the click. Soon you will be able to download this ASP.NET Web API Tutorial PDF.

Top 10 Interview Questions and Answers Series: