Create Simple Web API 2 Project in few minutes - .NET Framework - Tutorial NO. 01

In the first tutorial in this series, I will show you how to create simple Web API project

You can download the sample project file in here. Download "SimpleWebAPI"sample project
  • Open visual studio in your PC.  For this tutorial i will use Visual Studio 2017.
  • Go to File >  New > Project 
  • And Select Installed > Visual C# > Web > ASP.NET Web Application (.NET Framework)



  • Name your project as "SimpleWebAPI". Then choose file location to save your solution and click OK.
  • Next step is select template for your project.
  • For this tutorial i will create project without using any built in content like MVC or Web Forms project. Select Empty project template and select Web API for core reference. (See image below)


  • Then Click OK.
  • Than Visual Studio will create solution with your project.
  • It include files in following image

  • For first tutorial, I will create simple Web API 2 controller
  • For than Right Click on "Controllers" folder in solution explore > Add > Controller (See following Image) 
  • Select Web API 2 Controller - Empty and Click Add.


  • Then VS popup to insert controller name. Type "HomeController" and Click Add. 
  • It will create Empty class file with "using System.Web.Http" namespace.  You can see  our "HomeController" class is inheriting from "ApiController" abstract class.
  • It have all the properties and methods and we can define those in our class to create Web API.
  • Now we are going to create simple GET request in our project
  • Now add following code to "HomeController" Class.
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Net;  
 using System.Net.Http;  
 using System.Web.Http;  
 namespace SimpleWebAPI.Controllers  
 {  
   public class HomeController : ApiController  
   {  
     [HttpGet]  
     public IHttpActionResult Get()  
     {  
       return Ok("Hi, I am Ishara");  
     }  
   }  
 }  


  • In above code i created simple HTTP GET request. There are other methods like POST, PUT, DELETE i will describe in future tutorials. 
  • Remember i put "[HttpGet]" annotation top of the Get method. You don't need to put that on the method if the name of the action method include "Get" in the beginning. (Ex : GetID, GetName, GetProduct etc.) . Otherwise you have to put [HttpGet] on the action method.
  • Then Right-Click on your solution and click "Build Solution"
  • Debug project by pressing F5 key. In my configuration by default it opens with Google Chrome. You can select any browser that you want.
  • After opening the browser replace browser URL with "http://{your localhost}/apu/Home" (In my case 'http://localhost:60823/api/Home') 
  • Then you can see result like following image
  • By default Web API 2 return XML result.  That's the reason our output in contain XML codes.
  • In My next tutorial i will explain how to get JSON (Java Script object Notation) response from our Web API. Because now a days JSON is much popular than XML and  we are going to continue our tutorial with JSON

In next tutorial (Tutorial NO. 2), i will show you how to test Web API using postman and see our Web API request using browser inspection tools.

This is enough for the first tutorial please ask any problems regarding this post, code in the comment section. see you on next tutorial.






Comments

Post a Comment

Popular posts from this blog

ASP.NET Identity With Oracle Database

Catch Database Exceptions - DbEntityValidationException - Entity Framework