ASP. NET WEB API 2: Creating an API help page

Source: Internet
Author: User

Once you've created a new Web API service, it's good to build an API help page so that other developers 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 exceptionthe 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>        "" PublicKeyToken =""/> "NewVersion        ="" /> </      dependentassembly >

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. New " ~/app_data/xmldocument.xml " )));
then in Solution Explorer, right-click the project name, select Properties, select Generate, in output config item, tick the XML document file, modify the output path to App_data/xmldocument.xmlthen open the API controller file and add comments to the Controller's action method via XML document annotation (///)
/// <summary>///Query the product information for the specified ID/// </summary>/// <param name= "id" >Product ID</param>/// <returns>Check the product records</returns>[HttpGet] PublicProduct Get (intID) {    returnRepository. Products.firstordefault (p = P.productid = =ID);}
Recompile the run project, navigate to the Help page, and add the comment information to the help pageIf you want to hide information for an API interface on the help page, you can add the Apiexplorersettings attribute to the action and set the Ignoreapi property to True
/// <summary> /// Get Product List /// </summary> /// <returns>  </returns>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.  

ASP. NET WEB API 2: Creating an API help page

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.