简体   繁体   中英

asp.net mvc web api help page not populating for given route

I am having difficulties having the help page populate for a web api controller. The only method in the controller is the following:

public HttpResponseMessage Get(string p1,string p2= "blend", string p3 = "blend")

The method appears reflected in the help page with the signature:

GET api/mycontroller?p1={p1}&p2={p2}&p3={p3}

However none of my comments flow to the help page. For other simple controller Get(string id) the help page works ok.

I tried adding the following on the WebApiConfig.cs

config.Routes.MapHttpRoute( name: "mycontroller", routeTemplate: "api/mycontroller/{p1}/{p2}/{p3}", defaults: new { p1 = RouteParameter.Optional, p2 = RouteParameter.Optional, p3 = RouteParameter.Optional
} );

But still the help page is not getting populated with the comments written for summary, param description or return.

Instead of trying to solve this with convention based routing in the setup I would use attribute routing .

First enable it in the WebApiConfig.cs

config.MapHttpAttributeRoutes();

Then decore the method in the controller with the route

[HttpGet]
[Route("api/mycontroller")]
public HttpResponseMessage Get1(string p1, string p2= "blend", string p3 = "blend")
//Call this api/mycontroller?p1=valueOne&p2=valueTwo&p3=valueThree

[HttpGet]
[Route("api/mycontroller/{p1}/p2/p3")]
public HttpResponseMessage Get2(string p1,string p2= "blend", string p3 = "blend")
//Call this api/mycontroller/valueOne/valueTwo/valueThree

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM