博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
A simple Test Client built on top of ASP.NET Web API Help Page
阅读量:5334 次
发布时间:2019-06-15

本文共 2561 字,大约阅读时间需要 8 分钟。

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.

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

  • Scripts\WebApiTestClient.js
  • Areas\HelpPage\TestClient.css
  • Areas\HelpPage\Views\Help\DisplayTemplates\TestClientDialogs.cshtml
  • Areas\HelpPage\Views\Help\DisplayTemplates\TestClientReferences.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 Areas\HelpPage\Views\Help) 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.

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.

 

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.

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

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.

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

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.

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.

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

 

Update: The source code is now hosted at: 

转载于:https://www.cnblogs.com/simadi/p/5007608.html

你可能感兴趣的文章
Linux cp强制覆盖解决办法
查看>>
2013腾讯马拉松编程初赛3月21日1002
查看>>
isee图片专家批量处理图片大小教程
查看>>
java enmu 使用说明
查看>>
java连接Oracle数据库练习题
查看>>
java反射机制的实现原理
查看>>
js页面传值,c#接收中文出现乱码问题
查看>>
成都UBER优步司机第六组奖励政策
查看>>
软件项目管理的成功原则
查看>>
Java for LeetCode 068 Text Justification
查看>>
bzoj1015: [JSOI2008]星球大战starwar
查看>>
Docker安装MySQL
查看>>
zookeeper 服务端上下线,客户端感知
查看>>
Python-读文件
查看>>
Javascript:猜猜弹出的是啥?为啥?
查看>>
浮点型表示方法
查看>>
技巧:在 C/C++中如何构造通用的对象链表[转]
查看>>
eclipse下svn的使用
查看>>
ES6的小知识(前半部分)
查看>>
冒泡排序
查看>>