C # Advanced Series--webapi Interface test tool: Webapitestclient

Source: Internet
Author: User

Foreword: These two days in the whole WEBAPI service, because the caller is the Android client, the Android developer also does not understand C # syntax, the API inside interface also cannot give them to see directly, no way, only the entire detailed document Bai. Because the number of interfaces is a bit more, each interface to specify the interface function, parameter type, return value type and so on, write to write the blogger to provoke Mao, is this document not to write their own? Is there no such document display tool on the Internet? With these two questions, in the network world looking for, the network world is wonderful, as long as you attentively, always find more or less help! This is not the blogger found this useful component: Webapitestclient. It is very good for the function of the interface, parameter function and type, return value description and so on, and provide the function of test call, input test parameter send request, can get corresponding return result. Wow, this series of visual effects is not what bloggers need, but also write what documents, hurriedly use up!

WEBAPI Series Articles

    • C # Advanced Series--webapi Interface test tool: Webapitestclient
    • C # Advanced Series--webapi cross-domain problem solution: CORS
    • C # Advanced Series--webapi Identity Authentication solution: Basic BASIC Certification
    • C # Advanced Series--WEBAPI Interface transfer parameter no longer confused: The explanation of the transfer parameter
    • C # Advanced Series--webapi interface return value not confused: return value type detailed
    • C # Advanced Series--webapi Exception handling solution
    • C # Advanced Series--webapi Regional Area usage Summary
I. Introduction of Webapitestclient

1, the Webapitestclient component function mainly has the following several:

(1), the Webapi interface into the browser, to visualize the way out, such as we through the http://localhost:8080/Help this address in the browser to see all the API interface and interface of the detailed instructions, Eliminates the hassle of our handwriting interface documentation.

(2), be able to view the API's class description, method description, parameter description, return value description. It is only necessary for us to add a detailed comment to/// <summary> When defining the method, and the component automatically reads the contents of the comment on the interface.

(3), can modify the HTTP request header file head and the request body inside the parameters, specify the characteristics of sending HTTP requests, such as specifying our most common contenttype indicates the type of parameters.

(4), the component has the function of testing interface, the friend who used soup UI should know that through soup UI it is convenient to test WebService parameter and return value. Our webapitestclient can also implement similar functions and test the interface directly through the test button on the page.

2, Webapitestclient is an open source component. Open Source Address: https://github.com/yaohuang/WebApiTestClient

Second, webapitestclient display

First Impressions: interface list.

Click on an interface to view the interface details. For example, this article looks at the GET request's no parameter method, the lower right corner has the button to test the interface.

Click the "Test API" button

Click Send for Request

The second interface with parameters

Enter parameters manually to get the results returned

If the type of the parameter is an object, you can directly parse the/// <summary> callout above the class definition, as shown below

Because it is a POST request, if you need to perform parameter lengths and types, you can specify them by Content-length and Content-type. and specific parameters can be specified in different formats, such as Application/json and Application/xml

Get return value

third, the use of Webapitestclient1. How to introduce components

First, we need to define an API project

The components are then introduced through NuGet, such as. Remember to select the third one.

After the successful introduction, you will add some key files to the project:

    • Scripts\webapitestclient.js
    • Areas\helppage\testclient.css
    • Areas\helppage\views\help\displaytemplates\testclientdialogs.cshtml
    • Areas\helppage\views\help\displaytemplates\testclientreferences.cshtml
2. How to use component 1, modify the api.cshtml file

With the above steps, the component webapitestclient can be introduced in. Here's just one thing to do: Open the file (based on areas\helppage\views\help) api.cshtml and add the following:

    • @Html. Displayformodel ("Testclientdialogs")
    • @Html. Displayformodel ("Testclientreferences")

Add the following code for the api.cshtml file:

@using system.web.http@using Webapitestclient.areas.helppage.models@model helppageapimodel@{var description =    Model.apidescription; Viewbag.title = description. Httpmethod.method + "" + description. RelativePath;}<Linktype= "Text/css"href= "~/areas/helppage/helppage.css"rel= "stylesheet" /><DivID= "Body"class= "Help-page">    < Sectionclass= "Featured">        <Divclass= "Content-wrapper">            <P>@Html. ActionLink ("help page Home", "Index")</P>        </Div>    </ Section>    < Sectionclass= "Content-wrapper main-content clear-fix">@Html. Displayformodel ()</ Section></Div>@Html. Displayformodel ("Testclientdialogs") @section scripts{<Linkhref= "~/areas/helppage/helppage.css"rel= "stylesheet" />@Html. Displayformodel ("Testclientreferences")}
2. Configure the XML path to read annotations

In fact, through the above steps, our project can already run up, can also call the interface test. However, it is not possible to read the contents of the// <summary> comment. You need to do the following to configure the line.

(1) Configure the path to generate the XML. We're on the project. Right click → properties → generate tab configuration XML Path

(2) in the XML read path: In the HelpPageConfig.cs inside a sentence, specify the read path of the XML.

This code is as follows:

Config. Setdocumentationprovider (new Xmldocumentationprovider (HttpContext.Current.Server.MapPath (" ~/app_data/webapitestclient.xml ")));
3. Test interface
    /// <summary>    ///testing the API test Client/// </summary>     Public classTestchargingdatacontroller:apicontroller {/// <summary>        ///get all the data/// </summary>        /// <returns>Return Data</returns>[HttpGet] Public stringGetallchargingdata () {return "Chargingdata"; }        /// <summary>        ///get all the data for the current ID/// </summary>        /// <param name= "id" >parameter ID</param>        /// <returns>Return Data</returns>[HttpGet] Public stringGetallchargingdata (stringID) {return "Chargingdata"+ID; }        /// <summary>        ///Post Submission/// </summary>        /// <param name= "OData" >Object</param>        /// <returns>whether the submission was successful</returns>[HttpPost] Public BOOLPost (tb_charging oData) {return true; }        /// <summary>        ///put request/// </summary>        /// <param name= "OData" >Object</param>        /// <returns>whether the submission was successful</returns>[Httpput] Public BOOLPut (tb_charging oData) {return true; }        /// <summary>        ///Delete Operation/// </summary>        /// <param name= "id" >Object ID</param>        /// <returns>whether the operation was successful</returns>[Httpdelete] Public BOOLDelete (stringID) {return true; }    }    /// <summary>    ///Charging Object Entity/// </summary>     Public classtb_charging {/// <summary>        ///primary Key ID/// </summary>         Public stringID {Get;Set; } /// <summary>        ///Charging device Name/// </summary>         Public stringNAME {Get;Set; } /// <summary>        ///Charging Device Description/// </summary>         Public stringDES {Get;Set; } /// <summary>        ///creation Time/// </summary>         PublicDateTime Createtime {Get;Set; } }

At this point, the component is finished, and the rest is running. We can get this effect by knocking the address Http://localhost:8080/Help/Index or http://localhost:8080/Help in the URL. Don't try to try it

Iv. Summary

The above is a simple summary of the use of the next webapitestclient, if your webapi is also a separate service, you can work together as a help document, save yourself to write. Haha, don't thank me, please call me red scarf. Welcome to recommend. Demo download

C # Advanced Series--webapi Interface test tool: Webapitestclient

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.