My question would be is that, would consumers of your api figure out easily that the {ext}
is optional?...personally, I would prefer the default behavior...but anyways following are some workarounds that I can think of:
A quick and dirty workaround. Split the DoFoo into 2 actions like DoFoo() and DoFooWithExt maybe. Notice that I am using an attribute called
ApiExplorerSettings
, which is for HelpPage purposes. Example below:[HttpGet] [Route("Path/Foo")] [ApiExplorerSettings(IgnoreApi=true)] public HttpResponseMessage DoFoo() { return DoFooHelper(); } [HttpGet] [Route("Path/Foo.{ext}")] public HttpResponseMessage DoFooWithExt() { return DoFooHelper(); } private HttpResponseMessage DoFooHelper() { //do something }
Create a custom
ApiExplorer
(which HelpPage feature uses internally) and check for specific routes like the following and can decide whether to show the action or not for that particular route.// update the config with this custom implementation config.Services.Replace(typeof(IApiExplorer), new CustomApiExplorer(config)); public class CustomApiExplorer : ApiExplorer { public CustomApiExplorer(HttpConfiguration config) : base(config) { } public override bool ShouldExploreAction(string actionVariableValue, HttpActionDescriptor actionDescriptor, IHttpRoute route) { if (route.RouteTemplate.EndsWith("Path/Foo", StringComparison.OrdinalIgnoreCase)) { return false; } return base.ShouldExploreAction(actionVariableValue, actionDescriptor, route); } }
Get list of all
ApiDescription
from the defaultApiExplorer
and then filter out the descriptions which you do not like. Example:Configuration.Services.GetApiExplorer().ApiDescriptions.Where((apiDesc) => !apiDesc.RelativePath.EndsWith("Path/Foo", StringComparison.OrdinalIgnoreCase))