问题
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.
回答1:
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
来源:https://stackoverflow.com/questions/46001022/asp-net-mvc-web-api-help-page-not-populating-for-given-route