"WEB API Project Combat Dry Series"-API access client (Webapiclient for Mvc/webforms/winform) (iv)

Source: Internet
Author: User

These days are not updated mainly because there is no suitable backend framework to support our web API project demo, so delayed a few days, the latest code has been through SQLite + NHibernate + AUTOFAC to meet our basic demo needs.

In accordance with established requirements, our API will be available to a wide range of clients, these clients can be a variety of web sites, apps, or WinForm, WPF, Silverlight and other such applications, in the future may be a variety of IoT and other IoT devices applications, With its many advantages, the Restful API has been a fire in the era of mobile Internet, and will develop better in the next era of IoT.

is a simple one, our apiclient in this chapter will provide a set of fixed patterns that assist clients to access the APIs created in our previous chapters.

Basic HTTP Transport

In the actual project we define a apiclient static class library, which is further encapsulated by HttpClient and Microsoft.AspNet.WebApi.Client class library, specifically based on the API we wrote earlier.

Apiclient

Let's take a look at the method signature he provides.

Get/post

As the name implies, these two are used to complete the common httpget/httppost, but the post method is more than an object PostData parameter, which is used to send us to post to the API end of the data.

Execute

Is the basic method used by Get,post, and the operation of Get/post is done by him.

Parameter description:

The host address of the APIURL:API

Method path for Methodname:api

Query: A combination of search criteria that usually corresponds to the querystring part of the URL, which is the part of the URL that follows the question mark

Method: To use the HttpMethod, here support get,post,put,delete four kinds,

When Postdata:post/put, the data to be uploaded to the API end is usually an object, which is serialized as JSON by Json.NET before being sent.

Useendpointprefix: This refers to the URL of our API usually has a convention, that is, after the host part of the URL with api/as the beginning, so this is to say whether the URL uses the default of this habit, such as http://xxx.com/api/xxxx

Uploadfiles/downloadfile

These two can also be seen from the name and easily, how we upload and download files via our API, this common scenario is to update the avatar through the app, or to export a file from the API side.

The following example URL illustrates the part of the URL that we have in the parameters above

Query Object Introduction

In addition to the query object above, the others are easy to understand, in the absence of the query object, we have to spell out the parameters of the URL part, we usually need a string to splice, a better way is to define a dictionary to do automatic connection, here we for more convenient,

We define a query object, and here we should understand that the query object is to help us automatically process the parameter part of the URL, whose core base class is corequery.

Here we look at several built-in query

Loginquery

It provides the necessary parameters of our login interface, which we can use to directly new a loginquery, the method of incoming API call.

Sessionquery

This is the API we need to authorize the need to pass in the SessionKey parameter, usually our Basecontroller inside will design a sessionquery, when initiating the API request requests, will attach this sessionquery, He will automatically append sessionkey=xxx parameters to the URL of the API.

 Public class Sessionquery:corequery    {        "sessionkey")]          Public string Get Set ; }          Public sessionquery ()        {        }        public  sessionquery (sessionquery query)        {            this. SessionKey = query. SessionKey;        }    }

Listquery:

You can see from the following parameters the request to process the paging.

Corequery.parmsobj Property

In addition to the general parameters that need to be customized by a separate query object, for some very simple APIs, if we need to create a query for each API interface, wouldn't that be a hassle?

In response to this problem, we came up with an anonymous object to solve the problem, corequery below a Parmsobj property, in use can be

This way to dynamically add URL parameters, which greatly reduces the number of query classes we define.

A full map of the class library of Webapiclient

This webapiclient can be used well in MVC, WinForm and other. NET projects to help your client applications quickly access the APIs you develop.

This chapter code: Https://code.csdn.net/ODotNet/odn-webapiclient/tree/master

Also published to Nuget, you can add a reference to the project by Install-package Odn.webapiclient

PM> Install-Package Odn.WebApiClient

In today's mobile internet era, as the siege division of us, who do not want to write only a set of APIs can let our web, Android app, IOS app, IPad app, hybired app, H5 Web common logic? "WEB API Project REAL-combat Dry series" teaches you a step after step to teach you the solution.

WEB API Project Combat Dry series-API access client (Webapiclient for Mvc/webforms/winform) (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.