Thursday, 3 September 2020

Creating First WEB API Project from scratch ?

 For creating Web API project open your Visual Studio Editor and Create New Project as shown in below figure.


Now you have to select ASP.Net Web Application(.NET Framework) from the list of installed templates as shown in the figure below and select Next Button.


Now Configure Your New Project window is open so here write down the Name of Project into the Text box Project Name after that change the location where you want to save your project and choose the targeted .Net Framework from the Framework dropdownlist then click on Create button.


Now it will show you some options like Empty,Web Forms,MVC,Web API inside the "create a new ASP.NET Application" template so choose Web API here and click on Create button as shown in the figure below.

Now you can see into the figure below we have successfully create a WEB API project now we have to analyse the folder structure of WEB API project. Here you can see three main folders Controllers,Views and Models with some other folders created automatically by the .NET Framework. Now expand the Controllers folder and open the file ValuesController which is default controller in WEB API and it inherites from the Base ApiController Class.











Difference between WCF and Web API ?

 Firsty We have to Understand What is WCF ? WCF stands for Windows Communication Foundation is also used for creating RestFul and Non Restful services like SOAP Services.WCF is mainly used for creating services that are protocol independent or transport independent. For example if we have a scenario in which we have two clients one is .NET client and other one is JAVA client and both the clients want to access a single WCF service with diffrent protocol and message formates like JAVA Client wants Transport Protocol as HttP and message format XML for his simplicity whereas .NET Client want transport protocol to be TCP and message formate to be Binary for the performance point of view so in this scenario we Create a Single WCF service with two endpoints one for each client.


Below figure shows Single WCF Service with two End Points.


When to use WCF Over WEB API :- If you do'nt have any restriction about the .NET framework version that is limitation for .NET 3.5 then you have to create Restful Services in Web API rather than WCF because WCF required more configuration settings for Restful Services . If you have existing SOAP service and you want to create more clients for RESTful services then you have to use WCF services.
WCF does not provides any MVC type support whereas Web API supports MVC type architecture support.
WCF is best suitable choice if you are using .NET 3.5.
WCF supports multiple transport protocols like TCP,UDP and Named Pipes.Whereas WEB API supports only HTTP Protocols only.
WEB API supports convention based CRUD operations with the help of HTTP Verbs like GET,PUT,POST and Delete.
WEB API provides support for Content Negotiation like provides choice for JSON/XML/Other Formates for response.
WEB API is Tightly coupled with HTTP.
WCF is better options when you want Serialization for the data over the network.
WCF has rich data contract and it is easy to self hosting.
WEB API is completely Open Source so it is better choice for creating Restful Services over the .NET Framework.


ASP.Net WEB API Introduction

What is ASP.NET Web API ? Firsty We have to Understand What is API ? API Stands for "Application Programming Interface" is a set of software or piece of code which perform some specific   task and provides response in Universal format like (JSOn/XML/Other Formates) and It uses Client Server Constraints , In which Client sends request to the Server   for specific data defined into the Header and Server Process on the request and sends Response back to the Client. So Client perform his/her specific task without  interering from the server side and vise versa is also true.

Here Client may a Browser or it may be Console Application or it may be a web application User Interface or It may a Mobile Device or it may be IoT type things also.

 ASP.NET Web API :- ASP.NET Web API is a Http based service built on the top of the .NET Framework and these services deployed anywhere  on Internet and accessed by the clients like Browsers,Mobile Applications,Desktop Applications and IoT devices. It is Independent Software Architecture that use            Http protocol for sending and receiving data. It has a Client Server architecture in which Client sends request and server process on that request and sends response  back to the server in the Universal format like Json/XML/other formates.

ASP.Net Web API Sketch Diagram is shown below.


Explanation of above diagram :- In the above diagram we have write WebAPI for showing             details for voters based on the Areacode and we deployed this Web API on https://emsanalysis.com  on the Internet. In the above figure   there are four clients which are accessing these Web API by sending request https://emsanalysis.com/VoterDetails?AreaCode=12356  to server.  Server process on the request and response back to clients like Desktop Application,Mobile Devices,Iot,and Browsers. Response contains JSON data or XML data or any other formates which specify into the header and according to response data client parse the  data and display it.  The most common use of Web API is to build Restful Services.</strong> So now question is what is Restful Services ? REST stands for  Representational State Transfer is an architectural pattern that is mainly used for creating API that is based on HTTP protocol.

 REST includes some rules that is used for creation of API, which includes

        Client Server Pattern

        StateLess Requests

        Cacheability for Data

         HTTP Verbs for Communication

Client Server Architecure Rule:-   In this constraint Client Sends a request to Server and Server process on the Client request and sends response back to the clients so that in this scenario Client logic and server logic both are independent with each other.
Stateless Request Rule:-   Request between Client and Server should be stateless, it means no data or reuest should not save on server or can say that we should not be storing anything related to client on the server and communication between client and server must be Stateless for requests.The request from the client to the server should contain all the necessarry information to process the request. Ensure that each request should independently treated by the Server.
Cacheability for Data:-   Cacheability of data means to cache data which is frequent used by the client just like suppose you fetch List of departments in a oraganization so it will not change for a time so our services should have cache data for some time and when same request is generated by client then browser should serve data so that frequent access of server will reduce here and it will increase performance of API.
HTTP Verbs for Communication:-   In API firstly we have to understand about the Resources. So In API Resource means set of data we just want from the server for example List of All Departments (Departments) is the resource for us. Another one is List of all the Cities and their Postal Addresses is also the Resource. In API we have to write a method for resource so that we can access resource by a meaning full name like https://emsanalysis.com/Products.Here Products are the name of Resource.
HTTP verbs are the predefined set of rules which is used for accessing these resources so there are mainly used VERBS are
GET
PUT
POST
DELETE
GET is used for get the data from the server or we can say that it is the GET request for only fetch the list of data or some data from the server. For Example https://emsanalysis.com/Products is a GET request and it shows list of products to client.
POST is used for sending some data to server so we can add data into the body of message. so POST is generally send data for save or process data on server according to it.
PUT is used for sending some data to server for update data based on the conditions or data appended into the body of message. In simple we can say that it is UPDATE operation for WEB API.
DELETE is used for delete some data from the database server based on the key values appended into the URL or we can say that it is DELETE operation for WEB API.

Creating First WEB API Project from scratch ?

  For creating Web API project open your Visual Studio Editor and Create New Project as shown in below figure. Now you have to select  ASP.N...