How to add a Apicontroller help page to a Web project without adding an API core component Helppage

Source: Internet
Author: User

How to add a Apicontroller help page to a Web project without adding an API core component Helppage

Objective:

Asp.  The Web project in net can provide interfaces to other projects through the Apicontroller controller. While we are debugging these interfaces, we can choose Unit Test, Web page test (get interface), write test code or third party test tool, etc. (e.g. postman can send Get/post request).

But here's a new debugging tool: Microsoft provides a way to help develop a Apicontroller Controller description help page + Test (with third-party).

  

Note: During the actual use process, I found a problem, I created the WEBAPI project, added the API's help page to the NuGet Management Pack helppage core components, access the help page when the first index (http://localhost:27609/ Help) actually reported 404, after the successful access to the page of the project compared to find that the MVC router, because through the WEBAPI project only add API core components of the project, and did not automatically add to our MVC core components (routing service), and The controller's help page is implemented with the MVC Routing core component , so we need to manually fill in the Routing service:

1-Add class under the App_start folder: RouteConfig.cs

  

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using System.web.routing;namespace lambda_learn{public    class Routeconfig    {public        static void RegisterRoutes ( RouteCollection routes)        {            routes. Ignoreroute ("{resource}.axd/{*pathinfo}");            Routes. MapRoute (                name: "Default",                URL: "{controller}/{action}/{id}",                defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}            );}}}    

2-Register and configure the routing service in the project startup file: Global.asax:

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.Mvc;using System.web.routing;using system.web.security;using system.web.sessionstate;using System.Web.Http;namespace tttttt{ Public    class Global:httpapplication    {        void Application_Start (object sender, EventArgs e)        {            //code            to run at application startup Arearegistration.registerallareas ();             Code            globalconfiguration.configure (Webapiconfig.register) that needs to be supplemented;            Routeconfig.registerroutes (routetable.routes);   Code that needs to be replenished}}}    

So how do you put it in the absence of such a core component? In VS2015, when we create a new WEBAPI project, we need to hook the MVC and API core components together, and the project will automatically register the MVC-related routes and API-related routes for us. Such as:

Don't say much nonsense, please read the original text:

The following article was reproduced from: http://www.cnblogs.com/pmars/p/3673654.html

  How to add Helppage to your ASP.

Recently wrote some webapi, so need to get a set of API help document, Google a bit, found that this can be automatically generated, the following is how to automatically generate Helppage instructions.

Reference: Http://www.asp.net/web-api/overview/creating-web-apis/creating-api-help-pages

In fact, I also follow the above statement step by step, but the above is English, I carry out a simple translation, and, in some of the places have made changes, also recorded here!

1, create a new project: (Here is the MVC Web project, we have added the MVC core component)

   

Confirm New Project, right-click Project Properties, make sure our current project is Framework4.5 (4.0 and below the installation package below is not installed successfully, remember, this is the lesson of blood)

  

2, adding a reference package

Right-click Reference, select Manage NuGet packages (you have to configure this first, how to configure Baidu a bit OK)

  

Then search for Helppage, and find the first one that is both the ASP. Helppage package that we need to add

  

Click Install, the speed of this installation depends on your network, anyway I am slow, and sometimes have to install several times.

  

Now the help page has been generated, right-click on the Project-"debugging-" Start a new instance, then after the page URL to write/help look (note: Our port should not be the same, no harm)

  

At this point you will find that the page is nothing, no hurry, go down.

3. Modify the configuration file generation location

The first thing to note is that this package will generate all the documents based on the XML configuration file generated by the project, so you have to get your project to generate this XML file first.

Right-click the project selection properties, then generate a column inside the selection output to the XML document file, the path and name as you write, as long as the following configuration corresponds to the OK, my side directly with the default.

  

What we need to do is to write the path to the XML in the program, the configuration is OK, open the Areas/helppage/app_start/helppageconfig.cs page, a few paragraphs inside the register function to remove the comments, Then change to the path just now OK.

 1 public static void Register (Httpconfiguration config) 2 {3//uncomment the following to Use the documentation from XML documentation file. 4 CONFIG. Setdocumentationprovider (New Xmldocumentationprovider (HttpContext.Current.Server.MapPath ("~/bin/service.xml")))  ; Note: Here the path is just said address, you modify the same as OK 5 6//Uncomment the following to use "sample string" as the sample for all AC tions that has the string as the body parameter or return type. 7//Also, the string arrays is used for ienumerable<string>. The sample objects'll be serialized to different media type 8//formats by the available formatters. 9 CONFIG. Setsampleobjects (new Dictionary<type, object>//This segment needs to be liberated mainly to do the sample of the ten {typeof (S             Tring), "sample string"},12 {typeof (Ienumerable<string>), New string[]{"Sample 1", "Sample 2"}}13 });

    

4, add controller

OK, now that the basic conditions are ready, there is nothing left to show, we create a controller, then add an interface

  

After adding the controller, there will be some auto-generated API, now you can go to refresh the help page, to see if there is a stock ...

  

is not found that the API has been shown out, but there is no useful description on the page, we add on it can be

I only modified two functions to see the effect:

1//GET Api/crab 2//<summary> 3//This is         GET function without parameter 4//         </SUMMARY&G T 5         //<returns>function return value</returns> 6 public         ienumerable<string> Get () 7         {8< C6/>return New string[] {"value1", "value2"}; 9         }10         //GET api/crab/512//<summary>13//This is         GET function with a parameter named id14
   ///</summary>15//         <param name= "id" ></param>16//         <returns>function return value</returns>17 public         string Get (int id)             : {"value";         

    

OK, now the page has changed, you can according to your needs to write help page.

5, other styles of API

Can be found that the default generated are some restapi, I do not have a cold, so I would like to change the wording:

1 public class Crabcontroller:apicontroller 2     {3//         <summary> 4//         test Method 5//         </SUMMARY&G T 6//         <param name= "MSG1" > String 1</param> 7//         <param name= "MSG2" > String 2</param> 8//         <returns> two strings of connection strings </returns> 9         [System.Web.Http.AcceptVerbs ("GET")]10         [ System.Web.Http.ActionName ("TestMethod")]11 public         string TestMethod (String msg1,string msg2)         {13             return MSG1 + msg2;14         }     }

This time, we also need to modify the configuration file, or you will find that his API name into the controller's name

  

Locate the path App_start/webapiconfig.cs and modify the register function as follows:

1 public static void Register (Httpconfiguration config) 2         {3             config. Routes.maphttproute (4                 Name: "Defaultapi", 5                 routetemplate: "{controller}/{action}",//modified here 6                 defaults:new {id = routeparameter.optional}7             ); 8         }

Now let's refresh the page:

  

OK, that's what I want.

6. Modify the contents of the title display

In order to personalize, I need to revise the title and the introduction part of the content, find the file: area/views/help/index.cshtml, modify the part of the code:

 1 @{2 Viewbag.title = "Here is the Title to be modified"; 3 4//Group APIs by controller 5 Ilookup

The page now:

  

7, the pit inside.

I found a hole in it when I was using it. Files Found: Areas/helppage/modeldescriptions/modelnamehelper.cs

There is a code in it: generictypename = generictypename.substring (0, Generictypename.indexof ("));

This code, if there is no "in the Generictypename string, there is a problem, so I modified this code:

1 var index = generictypename.indexof ("'); 2                 generictypename = generictypename.substring (0, Index! =-1? in Dex:genericTypeName.Length);

8, not on the page display API

In fact, a lot of functions do not need to be published, then we can use the following code to set:

1 [Apiexplorersettings (ignoreapi=true)]2 public httpresponsemessage Get (int id) {  }

OK, this is basically the problem, now Helppage is ready to use, the next article I intend to write Test Client, that is, in the help page put a test tool, connected here: Http://www.cnblogs.com/pmars /p/3673811.html, so that we are in the development of the time is very convenient.

How to add a Apicontroller help page to a Web project without adding an API core component Helppage

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.