Top 20 ASP.Net Web API Interview Questions and Answers

In this blog, I have collected interview questions and answers of ASP.Net Web API.

1. What is ASP.Net Web API?

Web API is the Microsoft open source technology for developing REST services based on HTTP protocol. ASP.Net Web API is a framework for building, consuming HTTP based service. The advantage of Web API is that it can be consumed by a wide range of clients like a web browser and mobile applications. Read more about Web API.

2. Web API vs WCF REST API

It is difficult to say which one is better between Web API and WCF Rest service. This is why because both the technology have their own significant in the context of development, hosting and consuming the service.

WCF service is good for Message Queue, duplex communication, one way messaging.
Web API is good for HTTP based service.

One major advantage of Web API is it is the best fit with MVC pattern.

WCF supports SOAP and XML format, while Web API supports any media format including JSON, XML.

WCF is good for developing service oriented applications, while ASP.Net Web API is perfect for building HTTP services.

WCF supports HTTP, TCP, Named Pipes as protocol on another side Web API supports HTTP protocol only.

WEB API is easy for those developer who are already familair to MVC pattern.

WCF need lots of configuration to run, while Web API is simple and no configuration needed to run.

The advantage of Web API over WCF services.

The disadvantage of WCF over Web API is that WCF requires a lot of configuration to work, but in Web API it is simple and no extra configuration.

3. Advantages of using ASP.Net Web API

Advantages of ASP.Net Web API are -

Filters
OData
Routing
Model Binding
Easy with MVC

To know more about the benefits of ASP.Net Web API, View this blog - Benefits of using Web API

4. What are the various return types in ASP.Net Web API?

HttpResponseMessage
IHttpActionResult
void
Other Type - string, int, or other entity types

5. How to create a basic ASP.Net Web API in Visual Studio 2015 or 2017?

To create a Web API you visit my earlier blog - How to create a Web API?

6. What is ASP.Net Web API routing?

You can visit an earlier blog - Routing in ASP.Net Web API

7. What is Media type formatter in Web API?

ASP.Net Web API Media Type formatter

8. CORS issue in Web API?

CORS stands for Cross-Origin Resource Sharing. CORS resolve the same-origin restriction for JavaScript. The same Origin means that a JavaScript can only make AJAX call to the web pages within the same origin.

To enable CORS you must install CORS nuget package using Package Manager Console.
Now open WebAPIConfig.cs file

add config.EnableCors();

Then add EnableCors attribute to Controller class and define the origin

[EnableCors(origins: "<origin url>", headers: "*", methods: "*")]


9. How to secure an ASP.Net Web API?

Web API security means, you want to control your Web API and decide who can access the API and who can't access the Web API.

Your Web API can be accessed by anyone who knows the URL. This is not a good practice in the context of security.

There are various ways to secure Web API. Let's discuss one by one.

Form Authentication, Authorization Filter.

Read in details view this blog - ASP.Net Web API Security

10. Http Get vs Http Post

GET vs POST is one of the most important question asked in an interview.
Top 10 differences between Http GET and Http POST

11. Can we use Web API with traditional ASP.Net Forms?

Yes, Web API can be used with ASP.Net Forms.
You can add Web API Controller and manage to route in Application Start method in Global.asax file.

12. Exception filters in ASP.Net Web API

The Answer will be available soon

13. Can we return View from ASP.Net Web API?

No, it is not possible in Web API as Web API creates HTTP based service.
It is available in MVC application.

14. What's new in ASP.Net Web API 2.0?

New features included in Web API 2 are -

1. Attribute-based routing

        [Route("product/{productid}/category")]
        public string Get(int productid)
        {
            return "value";
        }

2. CORS (Cross-Origin Resource Sharing) support
3. OWIN to Self Host Web API
4. Web API OData

15. How to restrict access to methods with an HTTP verb in Web API?

You can just add an attribute as shown below -

       [HttpGet]
        public HttpResponseMessage Test()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            ///
            return response;
        }

       [HttpPost]
        public void Save([FromBody]string value)
        {

        }

16. How to make sure that Web API returns data in JSON format only?

Visit this blog - How to make sure that Web API returns JSON only.

17. How to give Alias name for an action method in Web API?

You can give Alias name by adding an attribute ActionName

       [ActionName("InertUserData")]
        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

18. Exception handling in Web API.

How we can handle errors in Web API?
Below classes helps to handle the exception in ASP.Net Web API.

  • ExceptionFilters
  • HttpError
  • HttpResponseException

19. How to host Web API?

The answer will be available soon.

20. How to consume Web API?

The answer will be available soon.


You may also like below blog-

ASP.Net Web API Tutorial
Learn WCF from beginning
ASP.Net Core Tutorial
Cloud Computing Tutorial
MicroServices Architecture


Keep following - SharePointCafe.Net

Comments

Popular

What's new in C# 6.0 language - All new features of C# 6.0 for developers

SharePoint Interview Questions and Answers