Question: Which .NET frameworks support Web API?
|
Answer: Following .NET frameworks support Web API:.
• .NET Framework 4.0 and later
• .NET Core 1.x and above.
• .NET 5 and later.
|
Question: What are variouu return types in Web API?
|
Answer: There are following four return types:
• Void: Returns an status code 204 without any content.
• HttpResponseMessage: Converts and sends a HTTP response message.
• IHttpActionResult: Calls ExecuteAsync internally to generate a HttpResponseMessage.
• Other type: Writes serialized return value into the response body and returns 200 (OK).
|
Question: How error handling is done in Web API?
|
Answer: Following are the ways to implement error handling in Web API:
• Using HttpResponseException: Through this exception class one can return HttpResponseMessage to the client. HTTP status code is returned what is specified in exception Constructor.
• Using HttpError: CreateErrorResponse creates an instance of HttpError object and returns meaningful error code and message to the client as HttpResponseMessage object.
• Using Exception Filters: Exception filters are used to catch unhandled exceptions in any action method in Web API.
• Using ExceptionHandler: Web API 2 provides a good way to achieve global exception handling using "ExceptionHandler" abstract class even to handle any exception that is not raised by an action method.
|
Question: What is middleware in Asp.Net Core?
|
Answer: Asp.Net Core introduced Middleware, which is a component (class) that is executed on every request.
It is similar to HttpHandlers and HttpModules classic Asp.Net. They can be framework provided middleware
or can be added via NuGet or can be developed custom middleware within the project.
One can configure middlewares within Configure method of the Startup class using IApplicationBuilder instance.
|
Question: What are some added features in ASP.NET Web API 2.0?
|
Answer: Below please find few useful features as introduced in Web API 2:
• CORS (Cross-Origin Resource Sharing)
• IHttpActionResult
• Web API OData
• OWIN (Open Web Interface for .NET) self-hosting
• Attribute Routing
|
Question: How one can test Web APIs?
|
Answer: The two most common ways to test Web APIs are:
• Using Fiddler: Fiddler comes as a free debugging proxy for the browsers. One can use it to compose and execute HTTP requests to Web APIs and then checking the HTTP response.
• Using Postman: Postman is popular as a free API debugging tool. One can simply install it on Chrome browser or on the Desptop to execute Web API calls.
|