Web API Ques Ans Google-2
Web API Ques Ans Google-2
Web API increases TDD (Test Data Driven) approach in the development of
RESTful services.
If we want to develop RESTful services in WCF, you surely need a lot of config
settings, URI templates, contracts & endpoints for developing RESTful
services using web API
It’s a not at all true that ASP.NET Web API has replaced WCF. In fact, it is another
way of building non-SOAP based services, i.e., plain XML or JSON string.
5) What are the advantages of Web API?
1|Page
Void – It will return empty content
HttpResponseMessage – It will convert the response to an HTTP message.
IHttpActionResult – internally calls ExecuteAsync to create an
HttpResponseMessage
Other types – You can write the serialized return value into the response
body
9) Web API uses which of the following open-source library for JSON
serialization?
The biggest disadvantage of this approach is that you cannot directly return an
error code like 404 error.
12) How do you construct HtmlResponseMessage?
For example:
1 Routes.MapHttpRoute(
2
3 Name: “ExampleWebAPIRoute”,
4
5 routeTemplate: “api/{controller}/{id}
6
7 defaults: new { id = RouteParameter.Optional}
3|Page
REST is used to make fewer data transfers between client and server which
make it an ideal for using it in mobile apps. Web API also supports HTTP
protocol. Therefore, it reintroduces the traditional way of the HTTP verbs for
communication.
16) How can we use Web API with ASP.NET Web Form?
17) How to you can limit Access to Web API to Specific HTTP Verb?
18) Can you use Web API with ASP.NET Web Form?
Yes, It is possible to use Web API with ASP.Net web form. As it is bundled with
ASP.NET MVC framework. However, it can be used with ASP.NET Web Form.
19) How Can assign alias name for ASP.NET Web API Action?
We can give alias name for Web API action same as in case of ASP.NET MVC by
using “ActionName” attribute as follows:
1 [HttpPost]
2
3 [ActionName(“SaveStudentInfo”)]
4
5 public void UpdateStudent(Student aStudent)
6{
7 StudentRepository.AddStudent(aStudent);
8}
TestApi is a utility library of APIs. Using this library tester developer can create
testing tools and automated tests for a .NET application using data-structure and
algorithms.
21) Explain exception filters?
4|Page
It will be executed when exceptions are unhandled and thrown from a controller
method. The reason for the exception can be anything. Exception filters will
implement “IExceptionFilter” interface.
22) How can we register exception filter from the action?
23) How you can return View from ASP.NET Web API method?
No, we can’t return a view from ASP.NET Web API Method. Web API creates
HTTP services that render raw data. However, it’s also possible in ASP.NET MVC
application.
1 Config.Routes.MapHttpRoute(
2
3 name: “MyRoute,”//route name
4
5 routeTemplate: “api/{controller}/{action}/{id}”,//as you
6 can see “API” is at the beginning.
7
8 defaults: new { id = RouteParameter.Optional }
9
5|Page
);
27) How can you handle errors in Web API?
Several classes are available in Web API to handle errors. They are HttpError,
Exception Filters, HttpResponseException, and Registering Exception Filters.
28) What New Features comes with ASP.NET Web API 2.0?
The latest features of ASP.NET Web API framework v2.0 are as follows:
Attribute Routing
Cross-Origin Resource Sharing
External Authentication
Open Web Interface NET
HttpActionResult
Web API OData
29) How can you restrict access methods to specific HTTP verbs in Web API?
With the help of Attributes (like HTTP verbs), It is possible to implement access
restrictions in Web API.
It is possible to define HTTP verbs as an attribute to restrict access.
Example:
1 [HttpPost]
2
3 public void Method1(Class obj)
4
5{
6
7 //logic
30) How can you pass multiple complex types in Web API?
6|Page
paramList.Add(p);
32) Name the tools or API for developing or testing web api?
REST is architectural style. It has defined guidelines for creating services which
are scalable. REST used with HTTP protocol using its verbs GET, PUT, POST and
DELETE.
We can perform a Unit test using Web API tools like Fiddler.
Fiddler –Compose Tab -> Enter Request Headers -> Enter the Request Body and
execute
35) How can we restrict access to methods with specific HTTP verbs in Web API?
Attribute programming is widely used for this functionality. Web API also allows
restricting access of calling methods with the help of specific HTTP verbs. It is
also possible to define HTTP verbs as attribute over method.
36) What is the usage of DelegatingHandler?
7|Page
9}
38) Tell me the code snippet to show how we can return 404 errors from
HttpError?
1 [NotImplExceptionFilter]
2
3 public class TestCustController :
4 Controller
5
6 { //Your code goes here }
43) By default, Web API sends HTTP response with which of the following status
code for all uncaught exception?
In WEB API HttpError used to throw the error info in the response body.
“CreateErrorResponse” method is can also use along with this, which is an
extension method defined in “HttpRequestMessageExtension.”
45) How can we register exception filter globally?
8|Page
Several classes are available in Web API to handle errors. They are HttpError,
HttpResponseException, Exception Filters, Registering Exception Filters.
47) What is the benefit of WebAPI over WCF?
WCF services use the SOAP protocol while HTTP never use SOAP protocol. That’s
why WebAPI services are lightweight since SOAP is not used. It also reduces the
data which is transferred to resume service. Moreover, it never needs too much
configuration. Therefore, the client can interact with the service by using the
HTTP verbs.
48) State differences between MVC and WebAPI
MVC framework is used for developing applications which have User Interface.
For that, views can be used for building a user interface.
WebAPI is used for developing HTTP services. Other apps can also be called the
WebAPI methods to fetch that data.
49) Who can consume WebAPI?
WebAPI can be consumed by any client which supports HTTP verbs such as GET,
PUT, DELETE, POST. As WebAPI services don’t need any configuration, they are
very easy to consume by any client. Infract, even portable devices like Mobile
devices can easily consume WebAPI which is certainly the biggest advantages of
this technology.
50) How can we make sure that Web API returns JSON data only?
To make Web API serialize the returning object to JSON format and returns JSON
data only. For that you should add the following code in WebApiConfig.cs class
in any MVC Web API Project:
C#
1 //JsonFormatter
2
3 //MediaTypeHeaderValue
4
5 Config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new
6 MediaTypeHeaderValue(“application/json”));
7
8 1
9
1 2
0
1 3
1
9|Page
1
2
1
3 //JsonFormatter
1
4 //MediaTypeHeaderValue
1
5 Config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new
1 MediaTypeHeaderValue(“application/json”))
6
1
7
10 | P a g e