ASP.NET WEB API Programming


ASP.NET WEB API.


In this blog post we will see a sample example to quick start ASP.NET WEB API Programming.


Before starting we will understand few important questions about the existence of the ASP.NET Web API.




Question 1 : Is ASP.NET WEB API is replacement of WCF ?


No,Web API has not replaced existing web service technology called WCF, but it's way different from WCF, where WCF follows SOAP based message formatting and Web API services  are non-SOAP based and returns plain XML or JSON string etc.

Web API transport data over network using HTTP Protocal unlike WCF. 

WCF uses different protocols for transferring data over HTTP, TCP, MSMQ, Named pipes.etc.  

Question 2 : ASP.NET Web API vs WCF RESTful Service  ?


Both ASP.NET Web API and WCF REST follows the REST principle but having following differences:

ASP.NET Web API

  • Web API supports full features of HTTP  compare to WCF REST.
  • Web API can be hosted on IIS as well as in an application.

WCF REST

  • WebHttpBinding used for creating  WCF RESTful Services.
  • HTTP Methods are mapped to attributes, for example, “WebGet” for GET method and “WebInvoke” for POST.

Question 3 : Difference between ASP.NET Web API & ASP.NET MVC ?


In my previous post we have discussed that purpose of Web API framework is to generate HTTP services that reaches more clients(mobile,desktop,tablet .etc) by generating data in raw format, for example, plain XML or JSON string.So, ASP.NET Web API creates simple HTTP services that generates raw data.

Whereas  ASP.NET MVC framework is used to develop web applications that generates Views as well as data. 

Let's  begin understanding Web API by simple example.


1) Open Visual studio (I have used Visual Studio 2017 Version)

2) Goto File-> New -> Project




3) Select Asp.net web application  from the list as shown in the above image.

4) Give  a name for your application, then click Ok.

5) In the next window select empty template to create stand alone application.




6) Open Solution Explorer, Right click on solution folder and select "Manage Nuget Packages".


7) Select Browse Tab in the window and enter search text as "webapi" and select "Microsoft.ASPNET.Webapi" from the list then click on Install/Download button.



8) After installing Web API package, goto solution explorer and add two folders for 
configuration  and controllers file. Name it as "Controllers" and "Configuration".

9) Goto Configuration folder and add a configuration file and give a  name to it as        "HelloWorldWebAPIConfiguration.cs"




10) add name namespace System.Web.Http in 


"HelloWorldWebAPIConfiguration.cs" class


11) Add the following lines of code in the configuration



using System.Web.Http;

namespace HelloWorldWebAPI.Configuration
{
public class HelloWorldWebAPIConfiguration
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{id}",
           defaults: new {id = RouteParameter.Optional
           });
}
}
}

12) Next Add  application file called "Global.asax" file ,go to project root folder and right click then select new item from the new item window select "Global Application Class" then click Ok.





13)  Web API routes to be configured when application starts. add below line in application_start() event of global.asax 


  GlobalConfiguration.Configure(HelloWorldWebAPIConfiguration.Register);




14) Add Api Controller, right click on controller folder then select add  --> New Item ->  select api controller template, Provide a name for the api controller.  


15) Web API Controller: Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send response back to the caller.

16) Remove predefined action methods from the class.. then include the below lines of code in the controller class


public string Get()

        {
            return "Hi World, Welcome to Web API";
        }

17) Build & Run your application and goto address bar in the browser and enter below url 


http://localhost:62107/api/HellowWorldWebApi


note : change port number according your application...


then you will see the below result in the XML format




The name of a controller class must end with "Controller" and it must be derived from System.Web.Http.ApiController class.


When we add a controller we can see the controller class loaded with pre defined action method syntax, and Controller class in Web API is inherited from ApiController and contains multiple action methods those names match with HTTP verbs like Get, Post, Put and Delete. see below picture.






[HttpGet] public IEnumerable<string> Values() { return new string[] { "value1", "value2" }; }



Interview Questions on WEB API.

1) Web API supports which  protocol?

2) Which  .NET framework supports Web API?

3) Types of routing supported in Web API 2?

Comments

Popular posts from this blog

Download Excel File using AJAX in JavaScript

How to Capture Image using web camera in Asp.net and C#.NET / VB.NET

How to Install GIT ?