Web API
Web API
Web API is an extensible framework for building HTTP based services that can be
accessed in different applications on different platforms such as web, windows,
mobile etc.
It works more or less the same way as ASP.NET MVC web application except that it
sends data as a response instead of html view.
It is like a webservice or WCF service but the exception is that it only supports
HTTP protocol.
WCF:
Supports HTTP, TCP, UDP and custom transport protocol
Uses attributes based programming model.
Uses Service, Operation and Data contracts.
Supports Reliable Messaging and Transactions.
Uses web.config and attributes to configure a service.
Supports RESTful services but with limitations.
REST:
REST represents REpresentational State Transfer; it is entirely a new aspect of
writing a web app.
REST is any interface between systems using HTTP to obtain data and generate
operations on those data in all possible formats, such as XML and JSON.
TestAPI:
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.
Property Description
DependencyResolver Gets or sets the dependency resolver for dependency
injection.
Filters Gets or sets the filters.
Formatters Gets or sets the media-type formatters.
IncludeErrorDetailPolicy Gets or sets a value indicating whether error details
should be included in error messages.
MessageHandlers Gets or sets the message handlers.
ParameterBindingRules Gets the collection of rules for how parameters should be
bound.
Properties Gets the properties associated with this Web API instance.
Routes Gets the collection of routes configured for the Web API.
Services Gets the Web API services.
API Routing:
Routing :
Web API routing is similar to ASP.NET MVC Routing. It routes an incoming HTTP
request to a particular action method on a Web API controller
Types:
Convention-based Routing
Attribute Routing
Convention-based Routing:
Web API uses route templates to determine which controller and action method to
execute.
At least one route template must be added into route table in order to handle
various HTTP requests.
Attribute Routing:
Attribute routing is supported in Web API 2.
As the name implies, attribute routing uses [Route()] attribute to define routes.
The Route attribute can be applied on any controller or action method
it must be enabled in WebApiConfig by calling config.MapHttpAttributeRoutes()
method.
Paremeter Binding:
def :A parameter binding is a piece of information that is transmitted from the
origin to the destination of a flow.
A parameter binding has a name and a value, which is obtained at its origin
component.
Types of Binding :
Complex Type :if the parameter type is complex type then Web API tries to get the
value from request body by default
Primitive Type :.NET primitive type such as int, bool, double, string, GUID,
DateTime,
decimal or any other type that can be converted from string type then it sets the
value of a parameter from the query string
QS-Query String
RB-Request Body
default rules for parameter binding
QS RB
GET PT,CT -
POST PT CT
PUT PT CT
DELETE PT,CT -
PATCH PT,CT -
[FromUri]:attribute to force Web API to get the value of complex type from the
query string
[FromBody] :attribute to get the value of primitive type from the request body
Void:
ex:consider the following Delete action method that just deletes the student from
the data source and returns nothing
JsonmediaTypeFormatters:
Type:appplication/json,text/json Description :handles json format
XmlMediatypeFormatters:
Type:appplication/xml,text/json Description :handles xml format
FormurlEncodedMediatypeFormatter:
Type:appplication/x-www-form-urlencoded,text/json Description :handles HTML form
URL-encoded data
JqueryMVCodedFormatter:
Type:appplication/x-www-form-urlencoded,text/json Description :handles HTML model
bound HTML form URL-encoded data
Filters:
Simple ,Action ,Authentication ,Autorization,Exception, Override
Authentication Filter:-->Interface:IAuthentication
Desc:Used to force users or clients to be authenticated before action methods
excute.
Authorizationfilter:-->Interface:IAuthorizationfilter--
>Class:AuthorizationAttribute
Desc:User restrict Access level
ExceptionFileter:-->Interface:Iexceptionfilter-->Class:ExceptionFilterAttribute
Desc:handles all unhandled exceptions in webapi.
overridefilter:Interfae:IOverrideFilter
Desc:Customize the behaviour of the Filter for individual method
Dependency Injection:
reduce the tight coupling among the software components .
reduce the hard-coded dependencies among your classes injecting those dependencies
runtime instead of design time technically.