zoukankan      html  css  js  c++  java
  • A simple Test Client built on top of ASP.NET Web API Help Page

    Step 1: Install the Test Client package

    Install the WebApiTestClient package from the NuGet Package Manager. Make sure to “Include Prerelease” then just type in “WebApiTestClient” and click Install.

    image

    Once the package is installed, it will add the following files to your project:

    • ScriptsWebApiTestClient.js
    • AreasHelpPageTestClient.css
    • AreasHelpPageViewsHelpDisplayTemplatesTestClientDialogs.cshtml
    • AreasHelpPageViewsHelpDisplayTemplatesTestClientReferences.cshtml

    It will also pull in the ASP.NET Web API Help Page package if it’s not already installed.

    Step 2: Wire up the Test Client on Help Page

    Open the file Api.cshtml (under AreasHelpPageViewsHelp) and add the following:

    • @Html.DisplayForModel("TestClientDialogs")
    • @Html.DisplayForModel("TestClientReferences")

    Note that you add the @Html.DisplayForModel("TestClientDialogs") after the <div> and @Html.DisplayForModel("TestClientReferences") inside the Scripts section.

    image

    You need to have the following JavaScript libraries installed. They should be installed if you used the default Web API template. Now, if you have different version numbers, you can update the references in TestClientReferences.cshtml.

    • jQuery 1.7.1
    • jQuery.UI.Combined 1.8.20
    • knockoutjs 2.1.0

    Once everything is wired up correctly, you should see the “Test API” button appearing on the bottom right corner of the API page.

    image

    Testing Web APIs

    Now you can click on the “Test API” button to start testing the APIs. For each URI parameters, it will automatically provide a text box so that you can fill in the value instead of editing the URI. Note that the URI automatically gets updated as you fill in the parameter values.

    image

    After you click on Send, another dialog will pop up showing you the response.

    image

    Adding Request Headers

    You can add additional request headers by clicking “Add Header”. For example, you can add the accept header “text/xml” to ask for XML.

    image

    And sure enough, your Web API returned the content as XML.

    image

    Providing Request Body

    For methods like POST, you can provide the body in the Body section. Note that it will be prepopulated with the samples from Help Page so you don’t need to do any copy-pasting.

    image

    And you can select different samples based on the supported media types. For instance, selecting application/xml will pull in the XML sample. Note that the content-type and content-length headers are updated accordingly.

    image

    You can change the content of the body and the content-length header will change as you type!

    image

    Update: The source code is now hosted at: https://github.com/yaohuang/WebApiTestClient

  • 相关阅读:
    【原创】整合Spring4+Hibernate4+Struts2时NullPointerException问题解决
    drools规则引擎与kie-wb和kie-server远程执行规则(7.18.0.Final)
    关于大龄程序员的感悟
    RocketMQ知识整理与总结
    Spring注解大全
    RocketMQ4.3.X关于设置useEpollNativeSelector = true报错问题
    RocketMQ4.3.x 史上配置最全详解,没有之一
    RocketMQ4.3.x对顺序消息的理解
    RocketMQ从3.5.8升级到4.3.2版本实战记录
    关于大表数据导出方案设想
  • 原文地址:https://www.cnblogs.com/simadi/p/5007608.html
Copyright © 2011-2022 走看看