Web API

1. What is REST and RESTFUL?

REST stands for Representational State Transfer. (It is sometimes spelled "ReST".) It relies on a stateless, client-server, cacheable communications protocol -- and in virtually all cases, the HTTP protocol is used.
REST is an architecture style for designing networked applications. The idea is that, rather than using complex mechanisms such as CORBA, RPC or SOAP to connect between machines, simple HTTP is used to make calls between machines.
RESTful applications use HTTP requests to post data (create and/or update), read data (e.g., make queries), and delete data. Thus, REST uses HTTP for all four CRUD (Create/Read/Update/Delete) operations.
RESTFUL is referred for web services written by applying REST architectural concept are called RESTful services, it focuses on system resources and how state of resource should be transported over HTTP protocol to a different clients written in different language.  In RESTFUL web service http methods like GET, POST, PUT and DELETE can be used to perform CRUD operations.

2. What is Web API? And Choosing which technology to use from WCF and Web API?

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

WCF is Microsoft’s unified programming model for building service-oriented applications. It enables developers to build secure, reliable, transacted solutions that integrate across platforms and interoperate with existing investments. (ASP.NET Web APIis a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework. 
Latest version Web API Web API 2.2

Choosing which technology to use

The following table describes the major features of each technology.
WCF
ASP.NET Web API
Enables building services that support multiple transport protocols (HTTP, TCP, UDP, and custom transports) and allows switching between them.
HTTP only. First-class programming model for HTTP. More suitable for access from various browsers, mobile devices etc enabling wide reach.
Enables building services that support multiple encodings (Text, MTOM, and Binary) of the same message type and allows switching between them.
Enables building Web APIs that support wide variety of media types including XML, JSON etc.
Supports building services with WS-* standards like Reliable Messaging, Transactions, Message Security.
Uses basic protocol and formats such as HTTP, WebSockets, SSL, JQuery, JSON, and XML. There is no support for higher level protocols such as Reliable Messaging or Transactions.
Supports Request-Reply, One Way, and Duplex message exchange patterns.
HTTP is request/response but additional patterns can be supported through SignalRand WebSockets integration.
WCF SOAP services can be described in WSDL allowing automated tools to generate client proxies even for services with complex schemas.
There is a variety of ways to describe a Web API ranging from auto-generated HTML help page describing snippets to structured metadata for OData integrated APIs.
Ships with the .NET framework.
Ships with .NET framework but is open-source and is also available out-of-band as independent download.
Use WCF to create reliable, secure web services that accessible over a variety of transports. Use ASP.NET Web API to create HTTP-based services that are accessible from a wide variety of clients. Use ASP.NET Web API if you are creating and designing new REST-style services. Although WCF provides some support for writing REST-style services, the support for REST in ASP.NET Web API is more complete and all future REST feature improvements will be made in ASP.NET Web API. If you have an existing WCF service and you want to expose additional REST endpoints, use WCF and the WebHttpBinding.

3. Explain the architectural style for creating web api?
The architectural style for creating web api are
  • HTTP for client server communication
  • XML/JSON as formatting language
  • Simple URI as the address for the services
  • Stateless communication
4. Mention what are the HTTP methods supported by REST?
HTTP methods supported by REST are:
  • GET: It requests a resource at the request URL. It should not contain a request body as it will be discarded. May be it can be cached locally or on the server.
  • POST: It submits information to the service for processing; it should typically return the modified or new resource
  • PUT: At the request URL it update the resource
  • DELETE: At the request URL it removes the resource
  • OPTIONS: It indicates which techniques are supported
  • HEAD: About the request URL it returns meta information
5. Mention what is the difference between SOAP and REST?

SOAP - 

  • SOAP is a protocol through which two computer communicates by sharing XML document
  • SOAP permits only XML
  • SOAP based reads cannot be cached
  • SOAP is like custom desktop application, closely connected to the server
  • SOAP is slower than REST
  • It runs on HTTP but envelopes the message
REST -
  •  Rest is a service architecture and design for network-based software architectures
  • REST supports many different data formats
  • REST reads can be cached
  • A REST client is more like a browser; it knows how to standardized methods and an application has to fit inside it
  • REST is faster than SOAP
  • It uses the HTTP headers to hold meta information
 6. Mention some key characteristics of REST?

Some key characteristics of REST includes
  • REST is stateless, therefore the SERVER has no state (or session data)
  • With a well applied REST API, the server could be restarted between two calls as every data is passed to the server
  • Web service mostly uses POST method to make operations, whereas REST uses GET to access resources

7. MVC Vs ASP.NET Web API?

The purpose of Web API framework is to generate HTTP services that reach more clients by generating data in raw format, for example, plain XML or JSON string. So, ASP.NET Web API creates simple HTTP services that renders raw data.
On the other hand, ASP.NET MVC framework is used to develop web applications that generates Views as well as data. ASP.NET MVC facilitates in rendering HTML easy.

8. How to Return View from ASP.NET Web API Method?

No, we can't return view from ASP.NET Web API method. As ASP.NET Web API creates HTTP services that renders raw data. Although, it's quite possible in ASP.NET MVC application.

9. How to Restrict Access to Web API Method to Specific HTTP Verb?

We should use verb on the Actions as in below code.
[HttpPost]
public void UpdateEmployee(Employee employee)
{
      EmployeeRepository.AddEmployee(employee);
}
10. Can we use Web API with ASP.NET Web Form?
Yes, ASP.NET Web API is bundled with ASP.NET MVC framework but still it can be used with ASP.NET Web Form.
It can be done in three simple steps as follows:
  1. Create a Web API Controller
  2. Add a routing table to Application_Start method of Global.asax
  3. Make a jQuery AJAX Call to Web API method and get data

11. Can We Provide an Alias Name for ASP.NET Web API Action?

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("SaveEmployee")]
public void UpdateEmployee(Employee employee)
{
      EmployeeRepository.AddEmployee(employee);
}


No comments:

Post a Comment