Q) What is ASP.NET Web API?(This is usually first question where interviewer will start)

A) ASP.NET web API is a framework which is used to develop HTTP services. These services can be used by many clients including browsers and mobile devices.

Note: In Interview you can elaborate the answer by explaining below advantages


Q) What are the Advantages of using ASP.NET Web API?

A) ASP.NET uses HTTP standards and uses HTTP standard verbs like GET, POST, PUT, DELETE, etc for CRUD operations.
Its Response can be generated in JSON or XML
It has complete support for routing
ASP.NET web API can be hosted in IIS or can be self hosted outside of IIS
It supports Model Bindings.


Q) What is Web API Routing?(The Interviewer may also ask as what is Routing)

A) Routing is a pattern matching according to registered route tables.

Example

Routes.MapHttpRoute(
Name:"WebAPIRouteTest",
routeTemplate:"api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }

Q) What is the benefit of using REST in Web API?(Here the question can be just explain REST)

A) Rest is used to make data transfers with minimum data which makes it an ideal for using in low bandwidth devices like Mobile Apps.


Q) How can we handle errors in Web API?

A) In Web API we can handle errors by using several predefined classes like HttpError, Exception Filters, HttpResponseException, etc.


Q) What is the difference between MVC and WebAPI?(This is the most commonly asked question in any interview)

A) MVC Framework is used for developing applications which have User Interface where as WebAPI doesnt have any User Interface.
In MVC we can return views but in WebAPI we can only return only data.


Q) What is the difference between Web API and WCF?(If you are experienced then this question can be expected)

A) Web API is a framework for building non SOAP based services, whereas WCF(Windows Communication Foundation) is designed to exchange Standard SOAP-based messages.

WCF can use variety of transport protocols like HTTP, TCP, NamedPipes etc, whereas Web API uses only HTTP protocol.


Q) Did Web API replace WCF?(Here the interviewer wants to know whether you know the difference between API and WCF or not )

A) No. Even though Web API has few advantages over WCF. WCF is still a better choice in following scenarios:
To use any other protocol like TCP or Named Pipes
Message Queuing scenario using MSMQ
One way or duplex communication


Q) What is the difference between ApiController and Controller?

A) API controller returns the serialized data and doesn’t return any views. Whereas the normal controller returns both.


Q) What are the new Features introduced in ASP.NET Web API 2.0?

A) Below are the new features added in ASP.NET Web API 2.0 framework:
Attribute Routing
External Authentication
CORS(Cross-Origin Resource Sharing)
OWIN(Open Web Interface for .NET)
Web API OData, etc


Q) How to restrict access to methods with specific HTTP verbs in Web API?

A) To restrict access to methods with specific HTTP verbs we have to define HTTP verbs as attribute over method as shown in below example:

[HttpPost]
public void SaveEmployee(Employee E)
{
AddEmployees(E);
}

Q) How to give Alias name for action methods in Web API?

A) The attribute “ActionName” can be used to give alias name for Web API actions. Below is an example:

[HttpPost]
[ActionName("SaveExployeeAlias")]
public void SaveEmployee(Employee E)
{
AddEmployees(E);
}

Q) What are Exception Filters?

A) Exception filters execute whenever controller methods throw unhandled exception. These Exception filters implement “IExceptionFilter” interface


Q) What are Authentication Filters in Web API?

A) Authentication Filters provides the authentication for the actions or methods in Controller.


Q) How can we pass complex types in Web API?

A) Arraylist and Newtonsoft JArray can be used to pass complex types. Below is an example:

ArrayList A1=new ArrayList();
Employee E=new Employee{EmployeeId=1, EmployeeName=”Emp1″};
Salary S=new Salary{SalaryId=1,EmployeeId=1,Salary=5000};
A1.Add(E);
A1.Add(S);