Skip to content

Web API Help Page - method paths and controllers grouping #54

Closed
@nomttr

Description

@nomttr

Hi, let's assume that i have two versions of HiController. I'm versioning my services by using URL path. In the ApiGroup.cshtml file i'm replacing {version} with the value of ApiVersionAttribute in order to get paths like these: GET api/framework/{version}/hello -> GET api/framework/1/hello. In the bellow configuration i'm expecting to get three paths but instead i'm getting only two. Is this a bug or misconfiguration? What's more, do you know how to group HiController and Hi2Controller in the one group "Hi"?

image

Controllers:

    [ApiVersion("1", Deprecated = true)]
    public class HiController : ApiController
    {
        [HttpGet]
        [Route("api/framework/{version:apiVersion}/hello")]
        public string Hi(){ /* ommitted */ }
    }

    [ApiVersion("2")]
    public class Hi2Controller : ApiController
    {
        [HttpGet]
        [Route("api/framework/hello")]
        [Route("api/framework/{version:apiVersion}/hello")]
        public string Hi() { /* ommitted */ }
    }

WebApiConfig

 public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.SuppressDefaultHostAuthentication();
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

            config.AddApiVersioning(options =>
            {
                options.ReportApiVersions = true;
                options.ApiVersionSelector = new CurrentImplementationApiVersionSelector(options);
                options.AssumeDefaultVersionWhenUnspecified = true;
            });

            var constraintResolver = new DefaultInlineConstraintResolver()
            {
                ConstraintMap =
                {
                    ["apiVersion"] = typeof( ApiVersionRouteConstraint )
                }
            };
            config.MapHttpAttributeRoutes(constraintResolver);

            // Web API routes
            //config.MapHttpAttributeRoutes();
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }

ApiGroup.cshtml

<table class="help-page-table">
    <thead>
        <tr><th>API</th><th>Description</th></tr>
    </thead>
    <tbody>
        @foreach (var api in Model.OrderByDescending(o => o.RelativePath))
        {
            if (api.RelativePath.Contains("{version}"))
            {
                var version = api.ActionDescriptor.ControllerDescriptor.GetDeclaredApiVersions().FirstOrDefault();
                if (version != null)
                {
                    string versionNumber;
                    if (version.MinorVersion != null && version.MinorVersion != 0)
                    {
                        versionNumber = string.Format(version.MajorVersion + "." + version.MinorVersion);
                    }
                    else
                    {
                        versionNumber = version.MajorVersion.ToString();
                    }

                    api.RelativePath = api.RelativePath.Replace("{version}", versionNumber);
                }
            }
            <tr>
                <td class="api-name"><a href="@Url.Action("Api", "Help", new { apiId = api.GetFriendlyId() })">@api.HttpMethod.Method @api.RelativePath</a></td>
                <td class="api-documentation">
                    @if (api.Documentation != null)
                {
                        <p>@api.Documentation</p>
                    }
                    else
                    {
                        <p>No documentation available.</p>
                    }
                </td>
            </tr>
        }
    </tbody>
</table>

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions