[go: up one dir, main page]

0% found this document useful (0 votes)
2 views4 pages

Web API

Web API is a framework for building HTTP-based services that can be accessed across various platforms, focusing on RESTful services and using routing and controller concepts similar to ASP.NET MVC. It differs from WCF in that it only supports HTTP and does not handle reliable messaging or transactions. The document also covers API routing, parameter binding, return types, media type formatters, filters, and dependency injection in Web API, providing a comprehensive overview of its configuration and functionality.

Uploaded by

guruguru2810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Web API

Web API is a framework for building HTTP-based services that can be accessed across various platforms, focusing on RESTful services and using routing and controller concepts similar to ASP.NET MVC. It differs from WCF in that it only supports HTTP and does not handle reliable messaging or transactions. The document also covers API routing, parameter binding, return types, media type formatters, filters, and dependency injection in Web API, providing a comprehensive overview of its configuration and functionality.

Uploaded by

guruguru2810
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

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 vs WEB API


Web API :
Supports only HTTP protocol.
Maps http verbs to methods
Uses routing and controller concept similar to ASP.NET MVC.
Does not support Reliable Messaging and transaction.
Web API can be configured using HttpConfiguration class but not in web.config.
Ideal for building RESTful services.

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.

RESTFUL: It is term written by applying REST architectural concepts is called


RESTful services.
It focuses on system resources and how the state of the resource should be
transported over HTTP protocol.

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.

Web API Configuration :


supports code based configuration. It cannot be configured in web. config file.
We can configure Web API to customize the behaviour of Web API hosting
infrastructure and components such as routes,
formatters, filters, DependencyResolver, MessageHandlers, ParamterBindingRules,
properties, services etc.

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.

public static class WebApiConfig


{
public static void Register(HttpConfiguration config)
{
// Enable attribute routing
config.MapHttpAttributeRoutes();

// Add default route using convention-based routing


config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}

name : name of the url


routetemplate : URL pattern ogf the route
defaults : An object parameter in includes default route values .
handler : the handler which which request will be dispatched .

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

Types Of Return Type:


Void
Primitive type or Complex type
HttpResponseMessage
IHttpActionResult--Same Like us MVC ActionResult, its From WEB API 2

Void:
ex:consider the following Delete action method that just deletes the student from
the data source and returns nothing

Media Types Formatters:

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

Simple Filter:--> Interface : IFilter


Description:Define the methods that are used in a Filter

Action Filter:--> InterFace:IActionFilter-->Class:ActionFilterAttribute


Desc:Added Extra logic for Before or after action methods excute

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.

Return Types in web API2


Return type How Web API creates the response
void Return empty 204 (No Content)
HttpResponseMessage Convert directly to an HTTP response message.
IHttpActionResult Call ExecuteAsync to create an HttpResponseMessage, then
convert to an HTTP response message.
Other type Write the serialized return value into the response body;
return 200 (OK).

You might also like