Objective
This article is of no reference value to the individual's review of Webapi. It mainly introduces the basic structure of the Web API and Webapi project, and creates a simple Webaapi project to implement CRUD operations.
In business applications, the correlation between applications is important, and applications such as mobile applications or single-page applications require powerful backend services to provide the appropriate data for CRUD operations.
Different WCF and WebApi
WCF is based on the SOAP protocol, supports a variety of transport protocols, a variety of encodings, hosted in the. NET Framework, need to produce WSDL proxy class files, more secure and reliable.
Webapi is based on an HTTP protocol that supports XML and JSON, and open source can be isolated from the. NET Framework. Rest style is more suitable.
Wcf |
ASP. NET Web API |
Supports multiple transport protocols (HTTP, TCP, UDP, and custom transport protocols), allowing switching between protocols |
Only HTTP protocols are supported. More suitable for transport from a variety of browsers, mobile apps. |
Supports multiple encoding formats (Text, MTOM, Binary), allowing switching between each other. |
Supports various media types, such as XML and JSON |
Compliant with Web service standards (reliable data, transmission, and data security) |
High standards of protocol, such as data reliability or transmission accuracy, are not supported. Based on basic protocols and typography, such as HTTP, WebSockets, SSL, JQuery, JSON, XML |
Support for request reply, single-line and multi-message exchange modes |
Supports HTTP request-response mode, but also supports extended integration, such as SIGNALR and WebSocket |
Based on the SOAP protocol, it needs to be described as WSDL, allowing automation tools to generate client proxies, even if the service contains complex schemas |
supports a variety of ways to describe the API. From the automatic generation of HTML Help page descriptions to the use of fabric metadata to integrate OData into APIs and more. |
Hosted in the. NET Framework |
Hosted in the. NET Framework but open source, can also be downloaded independently |
Use WCF to create a reliable, secure Web service that can support multiple transport protocols. Use WEBAPI to create an HTTP protocol-based service that serves a broader user base. When creating and designing new restful-style services, the WEBAPI is adopted. Although WCF also provides services that support write-rest style, WEBAPI support is better.
SOAP and ASP. NET Web Services
SOAP is a standard XML-based protocol that can communicate with HTTP. It can be understood that communication between programs is through a SOAP protocol in the form of XML. The ASP. NET Web Service provides the Web service that creates the SOAP protocol.
Problems with soap
- Metadata metadata will also be transferred, taking up the transfer space.
- The proxy class needs to be created on the client. When the service-side update service, the client should also update the proxy class in time.
REST
The rest protocol is used for data transfer in distributed environments, which enables us to treat distributed services as a resource and manipulate them through a simple HTTP protocol.
Rest corresponds to the database crud operations in 4 ways:
- Get: Equivalent to R in CRUD operations, data acquisition
- PUT: Equivalent to u in CRUD operations, data change
- POST: Equivalent to C in CRUD operations, data creation
- Delete: equivalent to D in CRUD operations, data deduplication
Examples Show
When the site is: www.cnblogs.com/blogs, the representative gets the blog list data
When the site is WWW.CNBLOGS.COM/BLOGS/1, it depends on the type, and if it is put, a new blog is created.
If it is post, the current blog is updated. If delete, the current blog is deleted.
Comparison between rest and soap
- Only the data being used is transmitted, and there is no meta data.
- The creation of the agent is omitted.
WCF Rest Services
WCF is later than the Web service. It provides a more secure and sophisticated way to create services. With WCF, we can define our own services and configure related protocols such as HTTP,TCP or IPC or even Message Queuing. WCF can also create rest services.
WCF creates a rest service that requires a lot of configuration work. Typically WCF is suitable for some special scenarios such as one-way message transfer, Message Queuing, and duplex communication. However, using WCF to create a rest service is too complex and limited to the. NET 3.5 framework.
Introduction to the ASP. NET Web API
Microsoft proposes that the ASP can be understood as a framework for creating restful services quickly and easily.
Getting Started with Web API creation
We can open the controller of values, it integrates Apicontroller, realizes the general Get,post,put,delete function. A description of the URL path for the response is provided.
Public classValuescontroller:apicontroller {//GET api/values Publicienumerable<string>Get () {return New string[] {"value1","value2" }; } //GET API/VALUES/5 Public stringGet (intID) {return "value"; } //POST api/values Public voidPost ([Frombody]stringvalue) { } //PUT API/VALUES/5 Public voidPut (intID, [frombody]stringvalue) { } //DELETE API/VALUES/5 Public voidDelete (intID) {}}
Open the RouteConfig.cs file under the App_start file to see the full API routing settings.
Public classRouteconfig { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); } }Realize your WEBAPI.
Public classUsercontroller:apicontroller { PublicIhttpactionresult Getread () {varEmployeelistviewmodel =NewEmployeelistviewmodel (); varEmpbal =NewEmployeebusinesslayer (); varEmployees =empbal.getemployees (); varEmpviewmodels = employees. Select (emp =NewEmployeeviewmodel {Name=EMP. Name, Salary=EMP. Salary.tostring (), Salarycolor= emp. Salary >15000?"Yellow":"Green" }). ToList (); Employeelistviewmodel.employees=Empviewmodels; Employeelistviewmodel.username=User.Identity.Name; returnOk (employees); }}
The above is the first step of the content, advanced address:
Http://www.cnblogs.com/ruanyifeng/p/5455105.html
Revisit ASP. WebAPI (a) First order