Joke Collection Website - Blessing messages - Online Interface Design —— How to design the interface project correctly and reasonably
Online Interface Design —— How to design the interface project correctly and reasonably
Because our entire WebAPI platform is developed on the basis of MVC, the interface of the entire WebAPI generally needs to be displayed and declared as [HttpGet] or [HttpPost] when it is defined. Although some interfaces do not need to be declared, it is beneficial to avoid the following error messages.
The requested resource does not support the http method "POST".
For example, the lookup object interface defined in the base class is as follows.
///& lt; Summary & gt
///Queries the database to check whether there is an object with the specified ID.
///& lt; /summary & gt;
///& lt; param name =“id“& gt; The ID value of the object.
///& lt; Returns & gt Returns the specified object if it exists, otherwise returns null.
【HttpGet】
public virtualtfindbyid(string id,stringtoken)
If you add, delete or modify interfaces, you usually need to submit data in POST mode, and for security reasons, you need to bring more parameters.
///& lt; Summary & gt
///Inserts the specified object into the database
///& lt; /summary & gt;
///& lt; param name =“info“& gt; Specified object
///& lt; Return & gt Whether the operation was successful. & lt/returns & gt;
【HttpPost】
public virtualcommonresultinsert(Tinfo,stringtoken,stringsignature,stringtimestamp,stringnonce,stringappid)
2. Interface definition of dynamic objects
In the general WebAPI interface, we may encounter many simple types of parameters, but we want them to submit data through POST, so we can deal with them in two ways. One is to define a class to place these parameters, and the other is to use dynamic JObject parameters. The former has many inconveniences, because we can't define one entity class for each interface parameter, so there may be many class definitions that are difficult to manage. For example, the following is a call interface case of WeChat API, and we also need to set such processing rules.
Interface call request description
Http request method: POST (please use https protocol)
Publishing data format: json
Sample post data: {"group": {"ID":108, "name": "test2 _ modify2"}}
So how do we adopt JObject? Let's look at the definition and processing code of the interface. Jobject is an object under the namespace.
///& lt; Summary & gt
///Modify the user password
///& lt; /summary & gt;
///& lt; param name =“param“& gt; A compound object containing a user name and a user password.
///& lt; param name =“token“& gt; User access token
///& lt; Returns & gt& lt/returns & gt;;
【HttpPost】
publicconresultmodifypassword(jobobjectparam,stringtoken)
{
//Token check. If it fails, an exception will be thrown.
CheckResultcheckResult = check token(token);
dynamicobj = param
if(obj! =null)
{
string username =;
string user password =;
Boolsuccess = BLLFactory & lt user & gt. (user name, user password);
ReturnnewCommonResult (success);
}
other
{
ThrownewMyApiException ("Error passing parameter");
}
}
Among them, when we convert the job object into the object we need, because we don't define the specific entity class, we use dynamic syntax to declare that it is a dynamic object, and the corresponding properties are obtained by the runtime.
dynamicobj = param
In this way, we can dynamically assign the JSON object corresponding to POST to the WebAPI interface when calling, without defining the classes of various interface parameters in advance.
///& lt; Summary & gt
///Call the WebAPI interface to modify the user password.
///& lt; /summary & gt;
///& lt; Paramname= "user name"> user name
///& lt; param name =“user password“& gt; Modify password
///& lt; Return & gt If the modification is successful, it will return true, otherwise it will return false.
public boolmodifypassword(stringuserName,stringuserPassword)
{
var action =“modify password“;
VarpostData = new
{
User name = user name,
User password = user password
}.to JSON();
string URL = GetTokenUrl(action);
CommonResultresult = JSON helper & lt; CommonResult & gt。 convert JSON(URL,post data);
Return (result! =null)? : false;
}
Among them, GetTokenUrl constructs a complete submission address according to parameters such as token and API address. We passed the code on it.
VarpostData = new
{
User name = user name,
User password = user password
}.to JSON();
You can dynamically create an object, generate its JSON string, submit the data to the corresponding API interface, and then convert the result into an object.
3, Settings and paging processing
In many interfaces, we need to use paging processing, and WebAPI is no exception. It can improve the efficiency of data retrieval, reduce the pressure of server data processing, and also improve the data display speed of client.
The universal collection interface is defined as follows (universal base class interface).
///& lt; Summary & gt
///Returns a collection of all objects in the database.
///& lt; /summary & gt;
///& lt; Returns & gt Collection of specified objects
【HttpGet】
publicvirtualList<T & gtget all(string token)
{
//Check whether the user has permission, otherwise a MyDenyAccessException exception will be thrown.
(,token);
List & ltt> list = ();
returnlist
}
However, there will be many such return records, and paging is generally needed, so the processing interface of paging is defined as follows.
///& lt; Summary & gt
///Query the database according to conditions and return a collection of objects (for paging data display).
///& lt; /summary & gt;
///& lt; Returns & gt Collection of specified objects
【HttpPost】
publicvirtualPagedList<。 T & gtFindWithPager(string condition,PagerInfopagerInfo,stringtoken)
Paging interface, in the results returned here, uses a generic class of PageList, which is convenient for us to get the current records and totals. Its definition is as follows.
///& lt; Summary & gt
///Paged collection
///& lt; /summary & gt;
///& lt; type param name =“T“& gt; target
publicclassPagedList<。 T & gt
{
///& lt; Summary & gt
///Returns the total number of records.
///& lt; /summary & gt;
Publicinttotal _ count {get setting; }
///& lt; Summary & gt
///List collection
///& lt; /summary & gt;
Public list <T & gtlist {get setting; }
}
Finally, the whole paging WebAPI interface is implemented as follows.
///& lt; Summary & gt
///Query the database according to conditions and return a collection of objects (for paging data display).
///& lt; /summary & gt;
///& lt; Returns & gt Collection of specified objects
【HttpPost】
publicvirtualPagedList<。 T & gtFindWithPager(string condition,PagerInfopagerInfo,stringtoken)
{
//Check whether the user has permission, otherwise a MyDenyAccessException exception will be thrown.
(,token);
List & ltT & gtlist =(condition, pageinfo);
//Structured into Json format for transmission.
varresult = newPagedList & ltT & gt(){total_count=,list = list };
Return the result;
}
Finally, the WebAPI code of the client calling paging is as follows.
///& lt; Summary & gt
///Query the database according to conditions and return a collection of objects (for paging data display).
///& lt; /summary & gt;
///& lt; param name =“condition“& gt; Query conditions
///& lt; param name =“pager info“& gt; Paging entity
///& lt; Returns & gt Collection of specified objects
publicvirtualList<T & gtFindWithPager(string condition,refPagerInfopagerInfo)
{
var action =“FindWithPager“;
string URL = GetTokenUrl(action)+(“& amp; condition={0}“,condition);
varpostData =();
List & ltT & gtresult = newList & ltT> ();
PagedList<T & gtlist = JsonHelper & ltPagedList<T & gt& gt。 convert JSON(URL,post data);
If (list! =null)
{
= _ count// Total number of modified records
Result =;
}
Return the result;
}
4. The hybrid framework interface integrates the WebAPI interface.
In the process of building the whole WebAPI platform and integrating the hybrid framework, I developed and integrated each module in a relatively independent way. They realized the unification of accessing the database directly, obtaining data through WCF service and obtaining data through WebAPI call, thus realizing the high integration of the whole hybrid framework.
The core of the whole hybrid framework is to integrate all reusable modules in a relatively independent way, and we can quickly build a unified application platform on a certain basis.
The entire WebAPI platform, including the server content, has published the corresponding WebAPI interface in the form of API controller.
In each independent module of the hybrid framework, we encapsulate the corresponding WebAPI client call processing, thus realizing the call mode of WebAPI.
Under Win 10, the mixed framework is run in WebAPI mode, and the main interface effects are as follows.
Independent module rights management system interface is shown below.
This series of articles is as follows:
Application of WebAPI Application Architecture in Winform Hybrid Framework (1)
Application of WebAPI Application Architecture in Winform Hybrid Framework (2)—— Handling of Custom Exception Results
Summary of WebAPI interface design experience
Application of WebAPI Application Architecture in Winform Hybrid Framework (3) —— Process Decomposition of WinFrom Interface Calling WebAPI
Application of WebAPI application architecture in Winform mixed framework (4)-Using code generation tools to quickly develop a complete set of applications.
Application of WebAPI application architecture in Winform mixed framework (5)-System-level dictionary and company-level dictionary coexist.
How to design an API interface, which requires authentication when requesting the interface to prevent the third party from calling the interface at will? How to use the interface:
The caller of 1. interface first applies for allocating clientid and clientsecret, and provides an accessible callback URL (for interface access_token) to the interface provider (server).
2. The interface caller uses clientid and clientsecret as parameters to send a request to the interface provider.
The interface provider accesses the callbackURL to send the access_token (there is a time limit and it will be retrieved after timeout).
3. Interface caller uses access_token as a parameter to call other interfaces to get relevant information.
4. After the access_token times out, the interface caller reacquires the access_token.
There is a problem:
Do you need this complicated mechanism just to prevent illegal users from using the interface at will?
This interface uses https connection to ensure data confidentiality.
So can you simplify the above process and only configure an access_token in the interface server?
Interface users only need to provide correct access_token to use other interfaces normally.
How to design an interface project correctly and reasonably to understand the prototype is actually more to help you design the data and structure you need to provide. But sometimes you don't have a prototype when you design, so this article is unnecessary. But if the prototype comes out after designing the interface, we can also use the prototype to verify whether the interface design is correct and reasonable.
- Related articles
- What if the telephone bill is staggered?
- How much does it cost to send a text message to a North Korean mobile phone?
- Do you need a mobile phone verification code for watermelon video withdrawal?
- How to send short messages to improve the credit line of CCB?
- How to cancel the return message sent by Fantasy Westward Journey?
- Hotel opening customers send flower baskets to congratulate.
- Distinguish the authenticity of text messages?
- If you don’t receive a text message after filling in your application, is it successful?
- How to close the credit card cashback of China Merchants Bank?
- How to turn off the mobile SMS of handheld TV?