Skip to main content

Asp.Net Web API



  • What is ASP.NET Web API?
ASP.NET Web API is a framework provided by the Microsoft with which we can easily build HTTP services that can reach a broad of clients, including browsers, mobile, IoT devices, etc. ASP.NET Web API provides an ideal platform for building RESTful applications on the .NET Framework. 
  • Difference between ASP.NET Web API and WCF
Web API is a Framework to build HTTP Services that can reach a board of clients, including browsers, mobile, IoT Devices, etc. and provided an ideal platform for building RESTful applications. It is limited to HTTP based services. ASP.NET framework ships out with the .NET framework and is Open Source. WCF i.e. Windows Communication Foundation is a framework used for building Service Oriented applications (SOA) and supports multiple transport protocol like HTTP, TCP, MSMQ, etc. It supports multiple protocols like HTTP, TCP, Named Pipes, MSMQ, etc. WCF ships out with the .NET Framework. Both Web API and WCF can be self-hosted or can be hosted on the IIS Server.
  • When to prefer ASP.NET Web API over WCF?
It totally depends upon the requirement. Choose ASP.NET Web API is you want only HTTP based services only as Web API is a lightweight architecture and is good for the devices which have limited bandwidth. We can also create the REST services with the WCF, but that requires lots of configuration. In case, if you want a service that should support multiple transport protocol like HTTP, UDP, TCP, etc. then WCF will be a better option.
  • With which .Net Framework version, Microsoft has introduced ASP.NET Web API?
First Version of ASP.NET Web API is introduced in .NET Framework 4. After that, all the later versions of the .NET Framework supports the ASP.NET Web API.
  • Can we consume ASP.NET Web API in applications created using other than .NET?
Yes, we can consume ASP.NET Web API in the applications created using another language than .NET but that application must have access/supports to the HTTP protocol.
  • What is the difference between ASP.NET MVC application and ASP.NET Web API application?
ASP.NET MVC is used to create a web application which returns both data as well as View whereas Web API is used to create HTTP based Services which only returns data not view. In an ASP.NET MVC application, requests are mapped to Action Methods whereas in the ASP.NET Web API request is mapped to Action based on the Action Verbs.
  • What are the RESTful Services?

REST stands for the Representational State Transfer. This term is coined by the Roy Fielding in 2000. RESTful is an Architectural style for creating loosely couple applications over the HTTP. In order to make API to be RESTful, it has to adhere the around 6 constraints that are mentioned below:

Client and Server Separation: Server and Clients are clearly isolated in the RESTful services.
Stateless: REST Architecture is based on the HTTP Protocol and the server response can be cached by the clients, but no client context would be stored on the server.
Uniform Interface: Allows a limited set of operation defined using the HTTP Verbs. For eg: GET, PUT, POST, Delete etc.
Cacheable: RESTful architecture allows the response to be cached or not. Caching improves performance and scalability.
Code-On-Demand
Layered System
  • What are the new features introduced in ASP.NET Web API 2.0?

The following features have been introduced in ASP.NET Web API 2.0:

  • Attribute Routing
  • CORS (Cross-Origin Resource Sharing)
  • OWIN (Open Web Interface for .NET) self-hosting
  • IHttpActionResult
  • Web API OData etc.
  • Can we return View from Web API?
No, Web API does not return View but they return the data. APIController is meant for returning the data. So, if you need to return a view from the controller class, then make sure to use or inherit the Controller class.
  • Does ASP.NET Web API replace the WCF?
No, ASP.NET Web API didn’t replace WCF Service as it is only used for creating RESTful Service i.e. non-SOAP based service.
  • What are Request Verbs or HTTP Verbs?

In RESTful service, we can perform all types of CRUD (Create, Read, Update, Delete) Operation. In REST architecture, it is suggested to have a specific Request Verb or HTTP verb on the specific type of the call made to the server. Popular Request Verbs or HTTP Verbs are mentioned below:

    • GET – The GET method is used to get or retrieve the information from the respective server using a given URI. 
    • HEAD – This is the same as GET, but transfers the status line and header section only. 
    • PUT – It is used for update and it replaces all current resources with the uploaded content. 
    • POST – A POST request is used to send data to the respective server. 
    • DELETE – Deletes or removes all current resources given by a URI. 
    • OPTIONS – Describes the communication options for the target resource. 
    • CONNECT – Establishes a tunnel to the server identified by a given URI. 
    • TRACE – Performs a message loop-back test along the path to the target resource. 

 

  • What are HTTP Status Codes?

HTTP Status Code Is 3-digit integer in which the first digit of the Status-Code defines the class of response. Response Header of each API response contains the HTTP Status Code. HTTP Status Codes are grouped into five categories based upon the first number.

S. No.
HTTP Status Code
Description
1.
1XX
Informational
2.
2XX
Success
3.
3XX
Redirection
4.
4XX
Client-Side Error
5.
5XX
Server-Side Error
Table: HTTP Status Code with Description

Some of the commonly seen HTTP Status Codes are: 200 (Request is Ok), 201 (Created), 202 (Accepted), 204 (No Content), 301 (Moved Permanently), 400 (Bad Request), 401 (Unauthorized), 403 (Forbidden), 404 (Not Found), 500 (Internal Server Error), 502 (Bad Gateway), 503 (Service Unavailable) etc.

  • What is Parameter Binding in ASP.NET Web API?

When Web API calls a method on a controller, it must set the values for the parameters, this particular process is known as Parameter Binding. By Default, Web API uses the below rules in order to bind the parameter:

FromUri: If the parameter is of “Simple” type, then Web API tries to get the value from the URI. Simple Type includes.Net Primitive type like int, double, etc., DateTime, TimeSpan, GUID, string, any type which can be converted from the string type.
FromBody: If the parameter is of “Complex” type, then Web API will try to bind the values from the message body.
  • What is Content Negotiation in Web API?

Content Negotiation is the process of selecting the best representation for a given response when there are multiple representations available. Two main headers which are responsible for the Content Negotiation are:

  • Content-Type
  • Accept
The content-type header tells the server about the data, the server is going to receive from the client whereas another way to use Accept-Header, which tells the format of data requested by the Client from a server. In the below example, we requested the data from the server in JSON format.
  • What is Media-Type Formatter in ASP.NET Web API?
Media-Type formatter is an abstract class from which JsonMediaTypeFormatter (handle JSON format) and XmlMediaTypeFormatter (handle XML format) class derived from. Media-Type formatter are classes responsible for serializing the response data in the format that the client asked for.
Standard definition: Media Type is a file identification mechanism on the MIME encoding system, formerly “MIME type”. The Internet media type has become the de facto standard for identifying content on the Internet. 
  • What is the use of Authorize Attribute?
Web API provided a built-in authorization filter, i.e. Authorize Attribute. This filter checks whether the user is authenticated or not. If not, the user will see 401 Unauthorized HTTP Status Code.
  • What is Basic HTTP Authentication?
Basic HTTP Authentication is a mechanism, where the user is authenticated through the service in which the client pass username and password in the HTTP Authorization request headers. The credentials are formatted as the string “username:password”, based encoded.
  • How Web API Routes HTTP request to the Controller ASP.NET MVC?
In ASP.NET Web API, HTTP request maps to the controller. In order to determine which action is to invoke, the Web API framework uses a routing table.
  • How many ways we can do Web API Versioning?

We can do Web API Versioning in the following ways:

  1. URI
  2. Query String Parameter
  3. Custom Header Parameter
  4. Accept Header Parameter
  • What is Exception handling?

Exception handling is a technique to handle runtime error in the application code. In multiple ways we can handle the error in ASP.NET Web API, some of them are listed below:

  1. HttpResponseException
  2. HttpError
  3. Exception Filters etc.

  • What is difference between SOAP and REST ? 

The difference between REST and SOAP is given below: 

    1. SOAP stands for Simple Object Access Protocol whereas REST stands for Representational State Transfer. 
    2. The SOAP is an XML based protocol whereas REST is not a protocol but it is an architectural pattern i.e. resource-based architecture. 
    3. SOAP has specifications for both stateless and state-full implementation, whereas REST is completely stateless. 
    4. SOAP enforces message format as XML whereas REST does not enforce message format as XML or JSON. 
    5. The SOAP message consists of an envelope which includes SOAP headers and body to store the actual information we want to send whereas REST uses the HTTP build-in headers (with a variety of media-types) to store the information and uses the HTTP GET, POST, PUT and DELETE methods to perform CRUD operations. 
    6. SOAP uses interfaces and named operations to expose the service, whereas to expose resources (service) REST uses URI and methods like (GET, PUT, POST, DELETE). 
    7. SOAP Performance is slow as compared to REST. 
  • What is REST and RESTful ?

REST or Representational State Transfer is an architectural style for designing applications. Instead of using complex mechanisms like CORBA, RPC or SOAP for communication, it dictates that HTTP should be used. 

There are a few principles associated with REST architectural style: 

    • Everything is a resource i.e. File, Video, Images, WebPage etc. 
    • Every Resource is identified by a Unique Identifier. 
    • Be Stateless- Every request should be an independent request. 
    • Use simple and Uniform Interfaces. 
    • Everything is done via representation (sending requests from client to server and receiving responses from server to client). 

RESTFUL: It is term written by applying REST architectural concepts and is called RESTful services. It focuses on system resources and how the state of the resource should be transported over HTTP protocol. 

Popular posts from this blog

Creating package in Oracle Database using Toad For Oracle

What are Packages in Oracle Database A package is  a group   of procedures, functions,  variables   and  SQL statements   created as a single unit. It is used to store together related objects. A package has two parts, Package  Specification  and Package Body.

Resolving 'Setup Account Privileges' error while installing SQL Server

A new installation of Microsoft SQL Server 2012 or Microsoft SQL Server 2008 R2 fails You see the following error message when you try to install a new instance of SQL Server 2012 or SQL Server 2008 R2: Rule "Setup account privileges" failed.

Creating Oracle stored Procedures using TOAD for Oracle

In a database management system, a  stored procedure  is a set of Structured Query Language (SQL) statements with an assigned name that's stored in the database in compiled form so that it can be shared by a number of programs. The use of  stored procedures  can be helpful in controlling  access to data, preserving  data integrity  and  improving  productivity.