Search code examples
c#asp.net-web-apiasp.net-web-api2asp.net-web-api-routingasp.net-web-api-helppages

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.


Solution

  • 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