API Is An Abbreviation For Application Programming Interface1
API Is An Abbreviation For Application Programming Interface1
For this, open Visual Studio 2013 for Web -> go to File menu and
select New Project.. This will open New Project popup as below.
TestApi?
TestApi is an API utility library. Using this library, tester developers can
design testing tools and automated tests for .NET applications that use
data structures and algorithms.
We can use the following third party tools for testing Web API.
• Fiddler
• Postman
• Note:
• Web API also supports routing same as ASP.NET MVC by including
action method name in the URL.
Routing can be implemented in two ways:
• Convention-based routing:
Attribute-based routing:
Note:
Post action method cannot include multiple complex type parameters
because, at most, one parameter is allowed to be read from the request
body.
Note:
The [FromBody] attribute can be applied on only one primitive parameter of
an action method. It cannot be applied to multiple primitive parameters of
the same action method.
1. Void
2. Primitive type or Complex type
3. HttpResponseMessage
4. IHttpActionResult
Void ;
void. If the return type is void , Web API simply returns an
empty HTTP response with status code 204 (No Content).
Type Primitive type or Complex type ;
The simplest action returns a primitive or complex data
type (for example, string or a custom object type).
Consider the following action, which returns a collection of custom Product
objects: C# Copy. [HttpGet] public List<Product> Get() => _repository. GetProducts()
HttpResponseMessage ;
A HttpResponseMessage allows us to work with the HTTP protocol
(for example, with the headers property) and unifies our return type.
In simple words an HttpResponseMessage is a way of returning a
message/data from your action.
the use of HttpResponseMessage?
IHttpActionResult ;
In Web API, IHttpActionResult's ExecuteAsync method is
automatically invoked when a client calls an action method,
allowing custom logic to generate a HttpResponseMessage
asynchronously.
Media-Type Formatters :
By using Media-Type formatters. Media type formatters
are classes responsible for serializing request/response data so
that Web API can understand the request data format and send
data in the format which client expects.
CRUD operation ;
CRUD stands for "Create, Read, Update, and Delete," which
are the four basic database operations. Many HTTP services
also model CRUD operations through REST or REST-like
APIs.
The HTTP POST request is used to create a new record in the data
source in the RESTful architecture.
The action method that will handle HTTP POST request must start
with a word Post. [HTTP POST]
In this article, we have used the localhost for Web API and called the GET
request.
Web API can be accessed in the server side code in .NET and also
on client side using JavaScript frameworks such as jQuery,
AnguarJS, KnockoutJS etc.
Here, we will consume our Web API (created in the previous section)
in the following environments:
1. Define the API endpoint: Know the URL of the Web API endpoint you want
to consume.
3. Make HTTP GET request: Use the HttpClient instance to make a GET request
to the Web API endpoint.
4. Process the response: Handle the response returned by the Web API, which
could be JSON, XML, or other formats.
IIS Hosting ;
Web API can be hosted under IIS, in the same way as a web
application. You have learned to create a Web API in the previous
section. As you have seen there, a Web API is created with
ASP.NET MVC project by default.
Self Hosting.
You can host a Web API as separate process than ASP.NET. It
means you can host a Web API in console application or windows
service or OWIN or any other process that is managed by . NET
framework.
-----------------*-------------------------
State differences between MVC and WebAPI
The MVC framework is used to create applications with user interfaces. Views
can be utilized to provide a user interface for this purpose.
WebAPI is used to create HTTP services. Other programs can also use the
WebAPI methods to retrieve the data.
What is SOAP?
SOAP is an XML messaging format that is commonly used in online service
interactions. It supports sending messages via HTTP or JMS, but additional
transport protocols are also supported. It is also an XML-based messaging
system used to exchange data between computers.
Example:
HttpPost]public void Method1(Class obj){//logic
Web service testing tools for REST APIs include:
• Jersey API
• CFX
• Axis
• Restlet