ASP. NET Web API Test help page build and test
Now it's becoming more and more popular to use Web APIs to develop.
In the development process of testing debugging, you can use tools such as fiddler to help Test, as well as:
There is a way to build a Help test page to help test the Debug API interface in ASP.
English Original address:
Http://blogs.msdn.com/b/yaohuang1/archive/2012/12/02/adding-a-simple-test-client-to-asp-net-web-api-help-page.aspx
Online friend's Chinese blog:
You will know exactly how to invoke your API interface. You can choose to build it yourself, but it would be better if you could generate it automatically. To simplify this task, the ASP. NET WEB API provides a library that automatically generates help pages at run time.
Add a help page to your project, first using NuGet to install the Microsoft.AspNet.WebApi.HelpPage library
After the installation is successful, the startup project may report an exception The reason for the exception is that the Microsoft.AspNet.WebApi.HelpPage library relies on the following assembly, and if the version of the Assembly used in the project is lower than the version it relies on, nuget upgrades the assemblies to the dependent version. The upgrade caused assembly conflicts between the local and GAC assemblies. The solution to this problem is to add the runtime node in the project's Web. config configuration file:
< dependentassembly> < assemblyidentity name = "System.Web.WebPages.Razor" PublicKeyToken = " 31bf3856ad364e35 "/> < BindingRedirect oldversion =" 1.0.0.0-3.0.0.0 "newversion =" 3.0.0.0 "/> </ D ependentassembly >
All code files for help pages are automatically generated under the Project Areas folder
Starting the project, the relative path to the help page is/help, and if you add a new API controller, the help page content is automatically updated at run time. The MVC view of the help page corresponds to the path in the project, and you can customize the layout, Introduction, title, style, and so on, as you wish. areas/helppage/views/help/index.cshtml The help page that is generated by default has a lot of placeholder strings that don't really matter. You can use the XML document annotations feature to create meaningful documents. To turn on this feature, you need to open the Areas/helppage/app_start/helppageconfig.cs file and uncomment the following code:
Uncomment the following to use the documentation from XML documentation File.config.SetDocumentationProvider (New Xmldo Cumentationprovider (HttpContext.Current.Server.MapPath ("~/app_data/xmldocument.xml"));
Then in Solution Explorer, right-click the project name, select Properties, select Generate, in the output configuration item, tick the XML document file, modify the output path to App_data/xmldocument.xml and then open the API controller file, via the XML document annotation method (///mode) Add a comment to the Controller's action method
<summary>///Query the product information for the specified ID///</summary>///<param name= "id" > Product ID </param>///<returns > The product record </returns>[Httpget]public product Get (int id) { return repository. Products.firstordefault (p = = P.productid = = id);}
Recompile the Run project, navigate to the Help page, and add the comment information to the help page if you want to hide information about an API interface on the help page, you can add the Apiexplorersettings attribute to the action
Ignoreapi Property set to True
<summary>///get a list of products///</summary>///<returns> Product List </returns>[apiexplorersettings ( Ignoreapi = True)]public IEnumerable < product> Get () { return repository. Products;}
You can also add this feature to the controller, and the information for the entire controller will not appear on the help page.
Note:
However, when using the routing feature, you should note that when you use a route map that is not configured, you cannot add the extension file name (. html. JSON), which can be used by default only with the normal URI
Such as:
Http://localhost:2424/api/test/action/params can test normally.
Http://localhost:2424/api/test/action/params.json not tested properly
Reference: http://www.mamicode.com/info-detail-500490.html
ASP. NET Web API Test help page build and test