Basics Of Web API

11:43

Introduction

Browser always preferred client by which we consumed data exposed over HTTP. So Microsoft introduced a features Web Service, WCF, Web API. But now Web API is more suitable than others because it is lightweight and when we need to use restful service it is more appropriate.

What Is Web API?

Web API not a part of the MVC Framework. It is a part of ASP.Net platform for building service over HTTP that can be expose service of clients including browsers, mobiles, iphone and tablets. It uses the same features like MVC such as routing, controllers, action, filter, model binders, dependency injection etc. We can used Web API as an stand-alone Web services application.

Why Web API?

WCF was brought into implement SOA, the intention was never to implement REST. WebAPI is built from scratch and the only goal is to create HTTP services using REST. Due to the one point focus for creating REST service, WebAPI is more preferred.

What are the advantages of using ASP.NET Web API?

1. It works the HTTP way using standard HTTP verbs like GET, POST, PUT, DELETE etc for all CRUD operations.
2. Complete support for routing.
3. Response generated in JSON or XML format using MediaTypeFormatter.
4. It has the ability to be hosted in IIS as well as self-host outside of IIS.
5. Supports Model binding and Validation.
6. Support for OData.

What new features are introduced in ASP.NET Web API 2.0?

1. Attribute Based Routing
2. External Authentication
3. CORS (Cross-Origin Resource Sharing)
4. OWIN (Open Web Interface for .NET) Self Hosting
5. IHttpActionResult
6. Web API OData

What Is CORS?

We'll start by creating two ASP.NET projects – one called "WebService", which hosts a Web API controller, and the other called "WebClient", which calls WebService. Because the two applications are hosted at different domains, an AJAX request from WebClient to WebService is a cross-origin request.

What is "Same Origin"?

Two URLs have the same origin if they have identical schemes, hosts, and ports.
These two URLs have the same origin:
    http://test123.com/employee.html 
    http://test123.com/admin.html

These URLs have different origins than the previous two:
    http://test123.net - Different domain
    http://test123.com:9000/employee.html - Different port
    https://test123.com/employee.html - Different scheme
    http://www.test123.com/employee.html - Different subdomain

What is OWIN?

Open Web Interface for .NET (OWIN) defines an abstraction between .NET web servers and web applications. OWIN decouples the web application from the server, which makes OWIN ideal for self-hosting a web application in your own process, outside of IIS.

What is OData?

The Open Data Protocol (OData) is a data access protocol for the web. OData provides a uniform way to query and manipulate data sets through CRUD operations (create, read, update, and delete).
ASP.NET Web API supports both v3 and v4 of the protocol. You can even have a v4 endpoint that runs side-by-side with a v3 endpoint.

Can we return View from ASP.NET Web API method?

No, we can't return view from ASP.NET Web API Method. ASP.NET Web API creates HTTP services that renders raw data.

How we can use same action for Create and Update in ASP.NET Web API?

We can provide an alias name for ASP.NET Web API action same as in case of ASP.NET MVC by using "ActionName" attribute as follows:
    [HttpPost]
    [ActionName("UpdateEmployeeInfo")]
    public void SaveEmployeeInfo(Employee emp)
    {
        EmployeeRepository.AddEmployee(emp); 
    }

How we can use Web API with ASP.NET Web Form?

Step 1: Create a Web API Controller.
Step 2: Add a routing table to Application_Start method of Global.asax.
Step 3: Make a jQuery AJAX Call to Web API method and get data.

Types Of Hosting In Web API

There are two types of hosting in Web API:
1. Self Hosting
2. IIS Hosting

Difference Between Web Service, Wcf, Wcf Rest and Web API

Web Service
1. It is based on SOAP and return data in XML form.
2. It support only HTTP protocol.
3. It is not open source but can be consumed by any client that understands xml.
4. It can be hosted only on IIS.
WCF
1. It is also based on SOAP and return data in XML form.
2. It support various protocols like TCP, HTTP, HTTPS, Named Pipes, MSMQ.
3. It is not open source but can be consumed by any client that understands xml.
4. It have IIS Hosting, Self Hosting, WAS (Windows Activation Service) Hosting, Windows Server.
WCF Rest
1. To use WCF as WCF Rest service you have to enable webHttpBindings.
2. It support HTTP GET and POST verbs by [WebGet] and [WebInvoke] attributes respectively. To enable other HTTP verbs you have to do some configuration in IIS to accept request of that particular verb on .svc files
3. It is not open source but can be consumed by any client that understands xml.
4. It have IIS Hosting, Self Hosting, WAS (Windows Activation Service) Hosting, Windows Server.
5. Passing data through parameters using a WebGet needs configuration. The UriTemplate must be specified
6. It support XML, JSON and ATOM data format.
Web API
1. This is the new framework for building HTTP services with easy and simple way.
2. It support various protocols like HTTP, HTTPS.
3. Web API is open source an ideal platform for building REST-ful services over the .NET Framework.
4. It can be hosted with in the application or on IIS.
5. Unlike WCF Rest service, it use the full featues of HTTP (like URIs, request/response headers, caching, versioning, various content formats)
6. It also supports the MVC features such as routing, controllers, action results, filter, model binders, IOC container or dependency injection, unit testing that makes it more simple and robust.
7. It is light weight architecture and good for devices which have limited bandwidth like smart phones.
8. Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.

Conclusion

I explain about Web API and why we use Web API where we already have Wcf. Guys your comments always welcome.

You Might Also Like

0 comments