ASP.Net Web API Interview Questions

    1 Votes

16) What is the difference between MVC Routing and Web API Routing?

There should be atleast one route defined for MVC and Web API to run MVC and Web API application respectively. In Web API pattern we can find “api/” at the beginning which makes it distinct from MVC routing. In Web API routing “action” parameter is not mandatory but it can be a part of routing.

17) Explain Exception Filters?

Exception filters will be executed whenever controller methods (actions) throws an exception which is unhandled. Exception filters will implement “IExceptionFilter” interface.

18) Explain about the new features added in Web API 2.0 version?

Below are the list of features introduced in Web API 2.0 –

  • OWIN
  • Attribute Routing
  • External Authentication
  • Web API OData

19) How can we pass multiple complex types in Web API?

Below are the methods to pass the complex types in Web API –

  • Using ArrayList
  • Newtonsoft JArray

20) Write a code snippet for passing arraylist in Web API?

Below is the code snippet for passing arraylist –

ArrayList paramList = new ArrayList();
Category c = new Category { CategoryId = 1, CategoryName = "SmartPhones"};
Product p = new Product { ProductId = 1, Name = "Iphone", Price = 500, CategoryID = 1 };

paramList.Add(c);
paramList.Add(p);

21) Give an example of Web API Routing?

Below is the sample code snippet to show Web API Routing –

config.Routes.MapHttpRoute(
name: "MyRoute",//route name
routeTemplate: "api/{controller}/{action}/{id}",//as you can see "api" is at the beginning.
defaults: new { id = RouteParameter.Optional }
);

22) Give an example of MVC Routing?

Below is the sample code snippet to show MVC Routing –

routes.MapRoute(
name: "MyRoute", //route name
url: "{controller}/{action}/{id}", //route pattern
defaults: new
{
controller = "a4academicsController",
action = "a4academicsAction",
id = UrlParameter.Optional
}
);

23) How we can handle errors in Web API?

Below are the list of classes which can be used for error handling -

  • HttpResponseException
  • Exception Filters
  • Registering Exception Filters
  • HttpError

24) Explain how we can handle error from “HttpResponseException”?

This returns the HTTP status code what you specify in the constructor. Eg :

public TestClass MyTestAction(int id)
{
TestClass c = repository.Get(id);
if (c == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return c;
}

25) How to register Web API exception filters?

Below are the options to register Web API exception filters –

  • From Action
  • From Controller
  • Global registration

26) Write a code snippet to register exception filters from action?

Below is the code snippet for registering exception filters from action –

[NotImplExceptionFilter]
public TestCustomer GetMyTestCustomer(int custid)
{
//Your code goes here
}

27) Write a code snippet to register exception filters from controller?

Below is the code snippet for registering exception filters from controller –

[NotImplExceptionFilter]
public class TestCustomerController : Controller
{
//Your code goes here
}

28) Write a code snippet to register exception filters globally?

Below is the code snippet for registering exception filters globally –

GlobalConfiguration.Configuration.Filters.Add( new MyTestCustomerStore.NotImplExceptionFilterAttribute());

29) How to handle error using HttpError?

HttpError will be used to throw the error info in response body. “CreateErrorResponse” method is used along with this, which is an extension method defined in “HttpRequestMessageExtensions”.

30) Write a code snippet to show how we can return 404 error from HttpError?

Below is the code snippet for returning 404 error from HttpError –

string message = string.Format("TestCustomer id = {0} not found", customerid);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, message);
Page 2 of 4

Popular Videos

communication

How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

C# Interview Questions
.Net Framework Interview Questions and Answers
ADO.Net Interview Questions and Answers for Freshers, Experienced
ASP.Net Interview Questions and Answers
ASP.Net MVC Interview questions and Answers
WCF Interview Questions and Answers
Top 50 Entity framework interview questions & answers