Top 10 ASP.NET Web API Interview Questions

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest

In this ASP.NET Interview Questions Series, so far we have covered questions related to the core of ASP.NET technology. In this part-6 of ASP.NET Web API Tutorial series, we will cover top 10 ASP.NET Web API interview questions related to ASP.NET Web API framework. What we have covered so far can be summarized as follows:

You can follow here to create your first Single Page Application using AngualarJS and Web API, covering all CRUD (Create/Retrieve/Update/Delete) Operations. Also, you can find the list of more Advanced AngularJS Interview Questions here.

So, let’s dive deeper to understand ASP.NET Web API technology concepts through Interview Questions.

For a comprehensive list of ASP.NET MVC Interview Questions, follow here.

ASP.NET Web API Interview Questions List

More Web API Interview Questions

You are working with WebDevTutorial as a Developer and designing an ASP.NET Web API application. You need to select an HTTP verb to allow blog administrators to moderate comments posted on Web Development Tutorial blog. Which HTTP verb should you use?

  • A. GET
  • B. POST
  • C. DELETE
  • D. PUT

For a complete online test and Practice Exams on Web Technologies, Click Here.

 Correct Answer: D

Foundation: HTTP and REST Concept

  • Resources and URIs
  • HTTP Methods
  • HTTP Status Codes
  • HTTP Content
  • Internet Media Types
  • REST
  • JSON & XML

HTTP (Hypertext Transfer Protocol) is an application level protocol that is used to deliver data such as html files, image files, query results, etc. on the World Wide Web. HTTP is stateless protocol means after one cycle of request and response the server forgets everything about the cycle, it considers another request from the same client as a new request from a new client.

Below is a simple Client-Server architecture of HTTP protocol.HTTP Architecture

Resource and URIs:

URI stands for Uniform Resource Identifier. URI is a string of characters used to identify a resource on the internet either by location or by name, or by both.

URI is used to identify resources, For example, In real world assume there is a person named “Muhammad Ahmad” who lives in “2807, Badar Street, Dubai”. We can find that person by name or by address or by both.

URI can be categorized into 2 as URL and URN

  • URL: Uniform Resource Locator
  • URN: Uniform Resource Name

Uniform Resource Identifier

URI is used to send requests to the server. It can be done with both URN and URL, but using URN is inefficient because there can be many resources with same name. So most commonly use method is URL and it consists of two required components; The Protocol & The Domain.

Look at the following URL:

http://www.webdevelopmenthelp.net/asp-net-mvc

Here the red colored part is the protocol and gold color part is the domain, other parts are optional. It may contain the port/path/query strings/fragments. 

HTTP Methods:

There are multiple HTTP methods which can be used according to the user requirements. Those methods are case-sensitive and they must used in uppercase.

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

Among these methods GET/PUT/POST/DELETE are the most popular methods.

HTTP Status Codes:

HTTP Status Codes are the 3 digit integers which contain in server response. There is a meaning for each number. Followings are the set of status codes with its meaning:

  • 1xx: Informational-It means the request has been received and the process is continuing.
  • 2xx: Success-It means the action was successfully received, understood, and accepted.
  • 3xx: Redirection-It means further action must be taken in order to complete the request.
  • 4xx: Client Error-It means the request contains incorrect syntax or cannot be fulfilled
  • 5xx: Server Error-It means the server failed to fulfill an apparently valid request.

HTTP Content:

There are more contents in a HTTP header field. HTTP header fields are components of the header section of request or response messages in the Hypertext Transfer Protocol. It defines the operating parameters of an HTTP transaction.

For example, HTTP request to fetch webapi.htm page from the web server running on webdevelopmenthelp.net.

Internet Media Types:

It 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.

For example, an instance of receiving an email from a server with an attachment, the server embed the media type of the attachment in the message header. So the browser can launch appropriate helper application or plug-in.

REST:

REST (Representational State Transfer) is an architectural style for designing applications and it dictates to use HTTP for making calls for communications instead of complex mechanism like CORBA, RPC or SOAP.

There are few principles associated with REST architectural style:

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

JSON & XML:

XML stands for eXtensible Markup Language, it is a markup language like HTML and designed to store and transport data. XML doesn’t do anything, but it store data in a specific format.

Following is a letter written from Ahmad to Hamza in XML format:

The above XML file represent the following message.

JSON (JavaScript Object Notation) also used to store and transport data. The above message can be transferred into JSON in following way:

Move on with ASP.NET Web API Interview Questions

Now, we are well familiar with HTTP and REST Concepts, let’s move on with ASP.NET Web API Interview Questions and Answers in details.ASP.NET Web API Interview Questions

What is ASP.NET Web API?

ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API we can create non-SOAP based services like plain XML or JSON strings etc. with many other advantages including:

  • Create resource-oriented services using the full features of HTTP.
  • Exposing services to a variety of clients easily like browsers or mobile devices etc.

ASP.NET Web API Interview QuestionsBack to top

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

Using ASP.NET Web API has a number of advantages, but core of the advantages are:

  • It works the HTTP way using standard HTTP verbs like GET, POST, PUT, DELETE etc for all CRUD operations.
  • Complete support for routing.
  • Response generated in JSON or XML format using MediaTypeFormatter.
  • It has the ability to be hosted in IIS as well as self-host outside of IIS.
  • Supports Model binding and Validation.
  • Support for OData.
  • and more….
For implementation on performing all CRUD operations using ASP.NET Web API, click here.70-486 Exam Preparation

Back to top

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

More new features introduced in ASP.NET Web API framework v2.0 are as follows:

  • Attribute Routing
  • External Authentication
  • CORS (Cross-Origin Resource Sharing)
  • OWIN (Open Web Interface for .NET) Self Hosting
  • IHttpActionResult
  • Web API OData

You can follow a good Web API new feature details on Top 5 New Features in ASP.NET Web API 2 here.
Back to top

What is Attribute Routing in ASP.NET Web API 2.0?

ASP.NET Web API v2 now support “Attribute Routing” along with convention-based approach. In convention-based routes, the route templates are already defined as follows:

So, any incoming request is matched against already defined routeTemplate and further routed to matched controller action. But it’s really hard to support certain URI patterns using conventional routing approach like nested routes on same controller. For example, authors have books or customers have orders, students have courses etc.

Such patterns can be defined using attribute routing i.e. adding an attribute to controller action as follows:

Back to top

What is External Authentication?

To reduce development time, external authentication services are currently available in abundance to help web developers when creating new web applications. Since most of the web users have several existing accounts for trendy web services and social media websites, when the authentication service from an external web service is used by a web application, time that would have been spent creating an authentication implementation get saved. Using an external authentication service saves end users from having to create another account for your web application, and also from having to remember another username and password.

In earlier days, developers had two choices – create their own authentication implementation, or to integrate an external authentication service into their applications. The user agent or web browser makes a request to a web application, through which web browser is redirected to external authentication service. The user sends their credentials to the external authentication service, and external authentication service checks the credentials for their authenticity and if they are successfully authenticated, the external authentication service will redirect the user to the original web application with some data which the external authentication service will send to the web application.

The web application will uses the data provided by the external authentication service to verify that the user has been successfully authenticated or not, and the web application may use the same data to collect more information about the user. Once the processing of the user’s information has been done by the application, the web application will return the appropriate response to the user based on its authorization settings.

Back to top

Explain briefly CORS(Cross-Origin Resource Sharing)?

ASP.NET WebAPI is a trending technology in today’s world. Everyone tries to access the service through AJAX requests on the server side. The problem is when a WebAPI is hosted and the other applications on different domains try to access the WebAPI through a request. Here, enabling CORS plays a vital role in WebAPI.

Browsers only allow a web page to make AJAX requests within the same domain. Its security prevents a web page from making AJAX requests to another domain. This is called origin policy.

CORS (Cross-origin resource sharing) is a technique that allows restricted resources on a web page to be requested from another domain outside the domain of which the first resource was served. A web page may freely attach cross-origin images, scripts, stylesheets, iframes, and videos. The same-origin security policy by default does not allow certain “cross-domain” requests, notably Ajax requests.

Cross-origin resource sharing provides a way by which a browser and server can interact to determine whether allowing the cross-origin requests are safe or not. It gives more functionality and freedom than same-origin requests and is more secure than simply allowing all cross-origin requests. For security reasons, browsers restrict all cross-origin HTTP requests initiated from scripts initially. For example, the same-origin policy is followed by XMLHttpRequest, a web application using this type of requests can only make HTTP requests to its own domain.

Back to top

Explain briefly OWIN (Open Web Interface for .NET) Self Hosting?

OWIN (Open Web Interface for .NET) is an interface for .NET between .NET web applications and web server. It is an open source technology. OWIN aims to decouple the connection between ASP.NET applications and IIS by giving a standard interface. Developers of web servers may be sure that, if they implement OWIN properly, ASP.NET applications can run on their server. Similarly, new web frameworks can be developed as a replacement to ASP.NET.

In addition to decoupling web frameworks and web servers, OWIN permits chaining along middleware into a pipeline. A web framework will move with OWIN while not knowing whether it’s interacting directly with the underlying web server, or with one or additional layers of middleware (each implementing OWIN) on top of the web server. This enables infrastructure issues, like authentication, to be split out into separate modules. This can be fascinating because it decouples them from the application’s own code, and makes them reusable across applications. Microsoft has now integrated number of OWIN modules as options that were only a part of the core ASP.NET framework in earlier days in its project named katana. This enables them to be reused in different web frameworks.

Web API Controller

Making Request:

Back to top

What is ASP.NET Web API OData?

The Open Data Protocol (OData) is a data access protocol created for the web. OData gives a consistent procedure to query and manipulates data sets via CRUD operations (Create, Retrieve, Update, and Delete). ASP.NET web API supports each of the v3 and v4 protocol.

One of the vital goals with this new implementation has been to support the side by side situation where customers will have v3 and v4 services running on a similar application. To that impact, we had to create some changes in the current naming of some classess and methods to permit for an inexpensive user experience.

To Create a Web API OData Solution, followings are the basic steps:

  • Create a Modal Class in Models with get set methods.
  • Add OData Controller.ASP.NET Web API OData
  • Set OData Route.
  • Set Get() method.

With ASP.NET Web API 2, support for $expand, $select, and $value options added for OData. These options simplify to control the representation that is returned from the server.

  • $expand: Normally, response doesn’t include related entities if we query an OData collection. By using $expand, we can get related entities inline in response.
  • $select: It’s used if we wanted to include subset of properites in response instead of all.
  • $value: It allows to return raw value of the property instead returning in OData format.

ASP.NET-Web-API-2

Back to top

WCF Vs ASP.NET Web API?

Actually, Windows Communication Foundation is designed to exchange standard SOAP-based messages using variety of transport protocols like HTTP, TCP, NamedPipes or MSMQ etc. On the other hand, ASP.NET API is a framework for building non-SOAP based services over HTTP only. WCF Vs WebAPIBack to top

WCF RESTful Service Vs ASP.NET Web API?

Although both WCF REST and ASP.NET Web API follows the REST architecture but these have follow differences:

WCF REST

  • Microsoft introduced “WebHttpBinding” to be used for creating WCF RESTful Services.
  • HTTP Methods are mapped to attributes, for example, “WebGet” for GET method and “WebInvoke” for POST.

ASP.NET Web API

  • As compared with WCF REST, Web API supports full features of HTTP.
  • Its possible to host Web API in IIS as well as in an application.

WCFREST Vs WebAPIBack to top

Is it true that ASP.NET Web API has replaced WCF?

It’s a misconception that ASP.NET Web API has replaced WCF. It’s another way of building non-SOAP based services, for example, plain XML or JSON string etc.
Yes, it has some added advantages like utilizing full features of HTTP and reaching more clients such as mobile devices etc.
But WCF is still a good choice for following scenarios:
  • If we intended to use transport other than HTTP e.g. TCP, UDP or Named Pipes.
  • Messag Queuing scenario using MSMQ.
  • One-way communication or Duplex communication
A good understanding for WCF(Windows Communication Foundation), please follow WCF Tutorial.

Back to top

MVC Vs ASP.NET Web API?

As in previous ASP.NET Web API Interview Questions, we discussed that purpose of Web API framework is to generate HTTP services that reaches 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.
MVC Vs Web API

For ASP.NET MVC Interview Questions, follow the link.
Back to top

Please look into following piece of code:

[ActionName(“GetById”)]

public ActionResult GetEmployeeById()
{
//Code logic here.

return View();
}
What’s true about the above code.

  • A. “GetEmployeeById” action method with be identified and called by name “GetEmployeeById”.
  • B. “GetEmployeeById” action method with be identified and called by name “GetById”.
  • C. Above code will generate an error because of wrong return type.
  • D. Action method can’t be called because of duplicate action method names.

For a complete online test and Practice Exams on Web Technologies, Click Here.

 Correct Answer: B

How to return View from ASP.NET Web API method?

(A tricky Interview Question) No, we can’t return view from ASP.NET Web API Method. As we discussed in earlier interview question about difference between ASP.NET MVC and Web API that ASP.NET Web API creates HTTP services that renders raw data. Although, it’s quite possible in ASP.NET MVC application.
Back to top

How to restrict access to Web API method to specific HTTP Verb?

Attribute programming plays it’s role here. We can easily restrict access to an ASP.NET Web API method to be called using a specific HTTP method. For example, we may required in a scenario to restrict access to a Web API method through HTTP POST only as follows:
Restrict Access in Web API

Back to top

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.
jQuery call to Web API for all CRUD (Create, Retrieve, Update, Delete) operations can be found here.

Back to top

How we can 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:

Web API Action Alias

ASP.NET Web API 2 Hands On
Building RESTful Web Service using ASP.NET Web API2 taking from beginner level and learn how to use the new features of Web API2 i.e. Attribute Routing etc. More Details

Back to top

What are Exception Filters? What are different ways to register exception filters?

Exception Filter is basically a class that implements IExceptionFilter interface. While working with ASP.NET Web API, there can be scenarios where the code can generate unhandled exceptions. And for those unhandled exceptions, client will be receiving same generic error i.e. “Internal Server Error”. In order to tackle such unhandled exceptions, Exception Filters can be used.

You can follow here for a detailed article on Exception Handling in ASP.NET Web API with following implementation:

  • What are Exception Filters in Web API?
  • How we can create a Custom Exception Filter?
  • How we can register Custom Exception Filter at different levels?

We can register exception filters for ASP.NET Web API in following different levels:

  • Register Exception Filter from action
  • Register Exception Filter from Controller
  • Register Exception Filter globally

Back to top

How we can guarantee that our Web API returns JSON data only?

In order to ensure that our ASP.NET Web API serialize the returning object to JSON format and returns JSON data only, we have to add following piece of code in WebApiConfig.cs class of our MVC Web API Project:Web API JSON Tutorial

Back to top

In this ASP.NET Web API Tutorial, we covered most important Interview Questions on ASP.NET Web API framework. Hopefully, it will be helpful for Web API developer Interview but along with these questions, do the practical implementation as much as you can. In Practical guide to ASP.NET Web API, you can find a good step by step approach for understanding and implementing ASP.NET Web API services.

Previous <<< ASP.NET Interview Questions – Part -5

Other Related Articles:

ASP.NET MVC & Web API Jobs [Updated Daily]

Top ASP.NET MVC Jobs

Developer, C#, .NET, MVC
Source: Logic20/20
Details: Developer, C#, .NET, MVC. C#, .NET framework (ASP.NET MVC, Entity Framework). Solid knowledge of C#, .NET framework (ASP.NET MVC, Entity Framework)....  More
13 days ago

Seattle, WA 98115 13-June-2017

Dot Net Developer
Source: Indeed
Details: ASP.NET MVC, JQuery, Entity framework, HTML programming. Develop enterprise web or desktop applications using *Microsoft .NET, ASP.NET, C#, application...  More
18 days ago

Redmond, WA 07-June-2017

Principal Engineer
Source: Indeed
Details: C#, .NET, ASP.NET MVC, WCF, Rest, WebAPI, HTML5, AJAX, JSON, JQuery, ServiceBus, RabbitMQ, TeamCity and similar....  More
30+ days ago

Austin, TX 22-May-2017

ASP.NET MVC Web Developer
Source: Cardinal Intellectual Property, Inc
Details: ASP.NET MVC Web Developer. Strong or expert knowledge of C#, JQuery, JavaScript, Knockout JS, ASP.NET MVC, MSSQL....  More
2 days ago

Evanston, IL 23-June-2017

Senior .Net Developer
Source: Indeed
Details: Sitecore (including Helix and MVC), C#/.NET, ASP.NET MVC, NPM, jQuery, CSS, SASS, GIT & SVN, CI, SQL, DB2, and REST service architecture....  More
4 days ago

Wilton, CT 21-June-2017

.NET MVC Web Developer
Source: R. Watson & Associates, Inc.
Details: Experience with ASP.NET MVC, SQL, HTML and JavaScript at a minimum. We are looking to expand our team by hiring a .NET MVC Web Developer....  More
10 days ago

Talent, OR 16-June-2017

.NET Developer
Source: Inforeem
Details: 3.AngularJS, ASP.NET MVC, JQuery, HTML5. We are looking for strong profiles in .NET/C#, MVC (data Structure, Algorithm), SQL queries....  More
24 days ago

Redmond, WA 02-June-2017

ASP.NET MVC Developer
Source: Net ESolutions Corporation (NETE)
Details: Design and develop new features and/or maintains existing applications developed on ASP.NET MVC. NETE is seeking a highly motivated, flexible, organized, and...  More
23 days ago

McLean, VA 03-June-2017

C# .Net Developer (Locals Only)
Source: Indeed
Details: 3.AngularJS, ASP.NET MVC, JQuery, HTML5. 1.Good in C# and Microsoft .Net....  More
3 days ago

Redmond, WA 22-June-2017

C# .Net Developer
Source: Indeed
Details: Responsibilities include: Rewrite an existing COTs system in accordance with CMS’ Agile XLC framework on site at Security Blvd. Under the guidance of the  More
3 days ago

Windsor Mill, MD 21244 22-June-2017

.Net Developer - Web Services - Web API - SQL
Source: Indeed
Details: 4 to 7+ years experience with software design and development (including the Microsoft application development framework) preferably C#, ASP.Net, ASP.Net MVC....  More
9 days ago

Minneapolis, MN 16-June-2017

Xamarin Dotnet/.Net Developer
Source: Indeed
Details: Strong knowledge and working experience of NET MVC. Solid experience on C#, Xamarin - Xamarin including Xamarin.iOS and Xamarin.Android, JavaScript, Mobile...  More
9 days ago

Miami, FL 16-June-2017

.NET Software Developer Fin Tech
Source: Indeed
Details: Experience with ASP.NET MVC. Would you enjoy working in a cutting edge C# environment and programming with technologies like C# 5.0, MVC 5, ASP.NET Core and...  More
9 days ago

Evanston, IL 16-June-2017

.NET Software Developer - Cambridge, MA
Source: Indeed
Details: Exposure to ASP.NET MVC framework:. Good understanding and exposure to ASP.NET MVC framework. .NET Software Developer....  More
11 days ago

Cambridge, MA 15-June-2017

C# .Net Developer
Source: Indeed
Details: MVC:. Solid understanding of MVC architecture and frameworks. Experience in enterprise web application development using Microsoft technologies(C#, ASP.NET MVC,...  More
12 days ago

Santa Clara, CA 13-June-2017

Software Developer
Source: Indeed
Details: Experience with Asp.net MVC, Razor, .Net Core, Html, CSS, XML, XSLT, XPath, AJAX, Web API (is a plus). Software Developer (Mid-Level)*....  More
3 days ago

Austin, TX 78730 22-June-2017

Software Developer
Source: Indeed
Details: Knowledge in Visual Studio 2005-2013 and .NET Framework 2.0 - 4.5.1, ASP.NET MVC 5.0, VB 6.0/.NET, C, C++, C#, Java, Tomcat, Transact-SQL, HTML, XML, JavaScript...  More
4 days ago

San Juan, PR 21-June-2017

ASP.NET Web Developer
Source: Sandhills Publishing
Details: ASP.Net MVC Framework, Object Oriented Programming, Web Services and RESTful APIs, XML, MS SQL, HTML, CSS, TFS or Version Control familiarity....  More
5 days ago

Lincoln, NE 20-June-2017

Applications Developer 3 - .NET
Source: Metropolitan Council
Details: Experience with any JavaScript based MVC or MVVM framework, such as:. Work in an environment that allows you to have a life outside of work and does NOT require...  More
17 days ago

Minneapolis, MN 08-June-2017

Software Development Engineer - Full Stack - Contract
Source: Neal Analytics
Details: Middle tier (ASP.NET MVC / Java). Familiar with MVC, Entity Framework, ASP.NET. We are supporting a well-known cloud platform for one of the largest software...  More
26 days ago

Redmond, WA 31-May-2017

SQL Lead Developer
Source: Indeed
Details: Understanding of ASP.NET MVC 5 including Bootstrap, Razor views and view models, as well as controllers and models. Are you a SQL Wizard?*....  More
9 days ago

Mooresville, NC 28117 16-June-2017

Junior Software Engineer
Source: Indeed
Details: Oracle 11g (or higher), SQL Server 2012 (or higher), AngularJS framework, HTML/HTML5, ASP.Net / ASP.Net MVC. Responsibilities of the Junior Software Engineer:....  More
2 days ago

Prescott Valley, AZ 86314 23-June-2017

Launch and Range Subject Matter Expert (Software Engineer)
Source: Sigmatech, Inc.
Details: Experience in software development utilizing C#, MVC, JavaScript, .NET., Visual Studio, and SQL. Proficiency in C#, ASP.NET MVC, Web API, MSSQL Server, MySQL,...  More
3 days ago

Colorado Springs, CO 80901 22-June-2017

Software Developer
Source: Indeed
Details: .NET Stack experience (C#, ASP.NET MVC, MS SQL Server). We are seeking a talented and motivated software developer to join our development team....  More
9 days ago

San Diego, CA 16-June-2017

Software Engineer. Net
Source: Indeed
Details: JQuery, SSRS, Design Pattern, ASP.NET MVC, and UML, Microsoft Test Manager. C#, ASP.NET with MVC, SQL Database 2012/2014, and Business Analysis, Report Services...  More
5 days ago

Boca Raton, FL 33487 21-June-2017

Top 10 Web Developer Interview Questions and Answers:

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInDigg thisPin on Pinterest