Learn Web API

Web API is a relatively broad concept. Here we mention Web API specific to ASP.NET Web API.

In this article we introduce the Web API of the main functions and compared with other similar frameworks , and finally through some relatively complex example shows how to build http services through Web API , but also shows the various Visual Studio .net building projects powerful.

 

What is the Web API

Official defined below, we emphasize two key points, which can be docked various clients (browsers, mobile devices), building http service framework.

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

Web API status in ASP.NET full frame as shown below, and SignalR together with a framework for building Service. Web API is responsible for building http regular service, but SingalR primarily responsible for building real-time services, such as stock, chat rooms, online games and other real-time requirements of high service.

Picture20

 

Why use Web API

Web API most important thing is to build for a variety of client services. Further except that the WCF REST Service, Web API utilizing various aspects of the protocol to Http expression service (e.g. URI / request response header / caching / versioning / content format), thus save a lot of configuration.

Picture2

 

When you encounter these following situations when you can consider using the Web API.

  • But need not require SOAP Web Service
  • We need to establish a non-soap-based http services on the basis of the existing WCF service
  • Just want to publish some simple Http service, you do not want to use relatively complex WCF configuration
  • Publishing services may be limited access bandwidth devices
  • We want to use open-source framework, the key when you can debug your own custom look or framework

Features

The main function of the Web API

1. supports Http verb (GET, POST, PUT, DELETE) of CRUD (create, retrieve, update, delete) operations

    It means different things in different http operation, thereby eliminating the need to expose the plurality of API support these basic operations.

2. Request reply by Http Status Code express different meanings, and the client can Accept header to the server to negotiate the format, for example, you want the server to return JSON format or XML format.

3. Request reply format support JSON, XML, and can be extended to add other formats.

4. primeval support OData .

5. Support Self-host or IIS host.

6. Most MVC support functions such as Routing / Controller / Action Result / Filter / Model Builder / IOC Container / Dependency Injection.

Web API vs MVC

You may feel MVC and Web API is very similar, any differences between them have it? First on the map, and this is their biggest difference.

Picture1

Detailed point that the difference between them,

  • MVC is mainly used to build websites, both concerned about the data also show concern page, and Web API only concerned with data
  • Web API supports the negotiation format, the client can inform the server through the Accept header format desired
  • Web API Support Self Host, MVC does not currently support
  • Web API express different actions (CRUD) through different http verb, MVC is expressed through action Action name
  • Web API built into ASP.NET System.Web.Http namespace, MVC located in System.Web.Mvc namespace, so the model binding / filter / routing functions differently
  • Finally, Web API is ideal for building mobile client service

Web API vs WCF

Publishing service between WCF Web API and how to choose? These rules provide a simple judgment here,

  • If the service needs to support One Way Messaging / Message Queue / Duplex Communication, select WCF
  • If the service requires TCP / Named Pipes / UDP (wcf 4.5), select WCF
  • If you need service on the http protocol and wants to use the various functions of the http protocol, select Web API
  • If the service needs to be called a variety of clients (especially mobile client), select Web API

Guess you like

Origin www.cnblogs.com/llsg/p/11249147.html