Web API concepts in different fields and Its implementation using Asp.Net MVC and Entity Framework . Web API Controller connects with Sql Server by Entity Framework. Web API Route can be used in different types of clients Like IOTs. ASP.NET Web API is a framework for building Web API’s, i.e. HTTP based services on top of the .NET Framework. The most common use case for using Web API is for building RESTful services.It has a great role for IOTs stands for Internet Of Things.
WEB API is a best fit to create a resource-oriented services using HTTP/Restful and it works well with MVC-based applications.WEB API can use any text format including XML and is faster than WCF.
Description
These Web API services can then be Used by below mentioned lists,
- Browsers
- Mobile applications
- Desktop applications
In Client-Server constraint, Client sends a request and the server sends a response. This separation of concerns supports the independent evolution of the client-side logic and server-side logic. In Stateless constraint, the client and the server must be stateless between requests. This means we should not be storing anything on the server related to the client. The request from the client should contain all the necessary information for the server to process that request. This ensures that each request can be treated independently by the server.
In Cacheable constraint, the client knows how long this data is good for so that the client does not have to come back to the server for that data over and over again. In Uniform Interface The HTTP verb like GET, PUT, POST, DELETE that is sent with each request tells the API what to do with the resource. Each resource is identified by a specific URI stands for Uniform Resource Identifier.
WCF was created to develop SOAP-based services and bindings. Since WCF is SOAP-based, which uses standard XML schema over HTTP, it could lead to slower performance. WEB API is a better choice for simpler, lightweight services. WEB API can use any text format including XML and is faster than WCF. WEB API doesn’t require any data contracts and doesn’t require configurations to the level of WCF.
Refer
To know more about Entity Framework, please visit my profile as mentioned below.
Steps To Be Followed,
Step 1
Create a table named Employee2 using the below script.
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[Employee2](
- [EmployeeID] [int] IDENTITY(1,1) NOT NULL,
- [FirstName] [nvarchar](50) NOT NULL,
- [LastName] [nvarchar](50) NOT NULL,
- [EmailID] [nvarchar](200) NULL,
- [City] [nvarchar](50) NULL,
- [Country] [nvarchar](50) NULL,
- PRIMARY KEY CLUSTERED
- (
- [EmployeeID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- GO
- SET IDENTITY_INSERT [dbo].[Employee2] ON
- GO
- INSERT [dbo].[Employee2] ([EmployeeID], [FirstName], [LastName], [EmailID], [City], [Country]) VALUES (1, N'Satyaprakash', N'Samantaray', N'[email protected]', N'Bengaluru', N'India')
- GO
- INSERT [dbo].[Employee2] ([EmployeeID], [FirstName], [LastName], [EmailID], [City], [Country]) VALUES (2, N'Satya', N'Saman', N'[email protected]', N'Bhubaneswar', N'India')
- GO
- INSERT [dbo].[Employee2] ([EmployeeID], [FirstName], [LastName], [EmailID], [City], [Country]) VALUES (3, N'sa', N'sa', N'[email protected]', N'bangalore', N'india')
- GO
- SET IDENTITY_INSERT [dbo].[Employee2] OFF
- GO
Step 2
- namespace SatyaWebApi
- {
- using System;
- using System.Collections.Generic;
- public partial class Employee
- {
- public int EmployeeID { get; set; }
- public string FirstName { get; set; }
- public string LastName { get; set; }
- public string EmailID { get; set; }
- public string City { get; set; }
- public string Country { get; set; }
- }
- }
Create an empty API Controller called "SatyaController.cs". Then, I added a new action to the API Controller for fetch data fromdatabaseand return to the client application.
Code Ref
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- namespace SatyaWebApi.Controllers
- {
- public class SatyaController : ApiController
- {
- public HttpResponseMessage Get()
- {
- List<Employee> allEmp = new List<Employee>();
- using (CrystalGranite2016Entities dc = new CrystalGranite2016Entities())
- {
- allEmp = dc.Employees.OrderBy(a => a.FirstName).ToList();
- HttpResponseMessage response;
- response = Request.CreateResponse(HttpStatusCode.OK, allEmp);
- return response;
- }
- }
- }
- }
- List<Employee> allEmp = new List<Employee>();
- public HttpResponseMessage Get()
- {
- //code here....
- }
- using (CrystalGranite2016Entities dc = new CrystalGranite2016Entities())
- {
- //code here....
- }
- List<Employee> allEmp = new List<Employee>();
- allEmp = dc.Employees.OrderBy(a => a.FirstName).ToList();
- using System.Net;
- using System.Net.Http;
- using System.Web.Http;
- <connectionStrings>
- <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-SatyaWebApi-20180326100928.mdf;Initial Catalog=aspnet-SatyaWebApi-20180326100928;Integrated Security=True" providerName="System.Data.SqlClient" />
- <add name="CrystalGranite2016Entities" connectionString="metadata=res://*/Satyadatabasemodel.csdl|res://*/Satyadatabasemodel.ssdl|res://*/Satyadatabasemodel.msl;provider=System.Data.SqlClient;provider connection string="data source=SODN-PAVILION\SQLEXPRESS;initial catalog=CrystalGranite2016;user id=sa;password=satya;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
- </connectionStrings>
- EntityFramework
- EntityFramework.SqlServer
Check out the WebApiConfig.cs to find out Web API routes or URL path of WEB API to get records in XML format.
- config.Routes.MapHttpRoute(
- name: "DefaultApi",
- routeTemplate: "api/{controller}/{id}",
- defaults: new { id = RouteParameter.Optional }
- );
OUTPUT
The Web API Route is : http://localhost:47250/api/satya .Here satya is name of API Controller name. I got all records in XML format in browser.

WCF
- WCF support multiple transport protocols (HTTP, HTTPS, Name Pipes, MSMQ, TCP, UDP, and custom transports) and allows switching between them.
- WCF doesn't support MVC features
- The Extension of WCF service is .svc.
- WCF support XML , JSON and ATOM Data format.
- WCF Supports Request-Reply, One Way, and Duplex message exchange patterns.
- WCF send the data asynchronously from one endpoint service to another service
- WCF can be hosted with in the application or on IIS or using window service.
- WCF Uses DataContractSerializer which is better in Performance as Compared to XmlSerializer.
- It can be hosted with in the applicaion or on IIS or using window service.
- Content format is SOAP+XML.
- Its Service interface is Service contracts.
- State management is Stateless with Per Call.
- Caching mechanism is Handled by application.
- Error handling type Faults, behaviors.
- Types is Opt-in.
- The ASP. NET Web API is a framework that uses the HTTP services and makes it easy to provide the response to the client request.
- Web API are a simple class file with .cs (C#) extension
- Web API can be hosted within the application or on IIS
- It is open source and can be consumed by any client that understands XML or JSON.
- It support the MVC feature such as routing, controllers , Action Results, model binders, IOC container or dependency injection , unit testing that makes it more simple and robust.
- HTTP is request/Response but additional patterns can be supported through SignalRand WebSockets integration.
- Web API light weight architecture and good for devices which have limited bandwidth like smart phones.
- Content format is Any media format.
- Its Service interface is URL Patterns, HTTP methods.
- State management is Stateless.
- Caching mechanism is Built in to HTTP Prefer application control.
- Error handling type HTTP status codes filters, exceptions.
- Types is Opt-out.
- Web Service is based on SOAP and return data in XML format only.
- SOAP(Simple Object Access Protocol) defines its own security.
- The extension of web service is .asmx
- Web Service support only HTTP protocol
- Web Service doesn't support MVC features
- It can be hosted only on IIS.
- What is Asp.Net Web API.
- How to implement using Entity Framework and MVC.
- Web API in Real-Time Scenario.
- Benefits Of Asp.Net Web API.

Meena MeenaPosted May 24, 2018, 5:10 AM
I have did the same process as above but getting error like 'The resource cannot be found.'
Sahil SharmaPosted Apr 19, 2018, 2:34 AM
Nicely written article, great job. I have one point to make about the choice of Web API over WCF. The points made for the difference between them are not sufficient to choose the framework. To me, there are projects where WCF is a better choice over WebAPI and vice versa. A very generic example is to use MSMQ for data saving. It can be achieved easily with WCF but not possible with WebAPI. Security Implementation is robust and well-tested in WCF where WebAPI doesn't have a standard security approach. However, for an API to be consumed over a lot of applications such as Mobile, IOT, Web; WebAPI is the better choice. So both of them are not to be compared with each other, instead use them both for robust applications.