When using lower case URL's in conjunction with the special [controller] string in the route like so:
services.ConfigureRouting(
options =>
{
options.LowercaseUrls = true;
});
[Route("bar/[controller]")]
public class FooController : Controller {}
The generated URL's that Swagger displays has an upper-case controller name:
bar/Foo
Wasn't sure whether to comment here or open a new ticket... considering the default for ASP.NET Core is now camel case, shouldn't camel-case controller names now be the default?
I even explicitly have
services.AddMvc()
.AddJsonOptions(opt =>
{
opt.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver();
});
in my Startup.cs yet I'm getting PascalCased controller names in Swagger (though property names are indeed camelCase).
Is there currently a way to get camelCase controller names and still use the generalized [Route("api/[controller]")] on my controllers?
What is the status on this? I'm still getting Pascal case.
I too wished that the document generator followed the routing configuration.
As a workaround I use the suggested fix in https://github.com/domaindrivendev/Swashbuckle/issues/834 (it just needs some using tweaks)
I found the UI will output api/foo given:
public class fooController : Controller { }
I was running into the same issue using dotnetcore 2.1.
I wouldn't doubt that there is a better way, but this worked for me.
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger(c =>
{
c.PreSerializeFilters.Add((document, request) =>
{
var paths = document.Paths.ToDictionary(item => item.Key.ToLowerInvariant(), item => item.Value);
document.Paths.Clear();
foreach (var pathItem in paths)
{
document.Paths.Add(pathItem.Key, pathItem.Value);
}
});
});
}
+1 to @wickdninja the code snippet can be simpler
app.UseSwagger(o =>
{
o.PreSerializeFilters.Add((document, request) =>
{
document.Paths = document.Paths.ToDictionary(p => p.Key.ToLowerInvariant(), p => p.Value);
});
});
Swashbuckle pulls a lot of it's metadata from ApiExplorer - the metadata layer that's built into ASP.NET Core, including RelativePath. It seems to me that root cause of your issue is with ApiExplorer and the fact that it assigns ApiDescription.RelativePath without accounting for configured RouteOptions.LowercaseUrls`. I'd like to create an issue in the ASP.NET Core repo as I'd prefer to solve it at source rather than a downstream library like Swashbuckle.
On further reflection, I'm not convinced ApiExplorer _ought_ to honor the RouteOptions.LowercaseUrls flag. From my understanding, that flag is used to indicate the casing of _generated_ URL's - i.e. when using UrlHelper.RouteUrl in razor pages etc. but doesn't have any baring on the routing of incoming requests to actions. Even with this flag set, the framework will ignore casing for incoming requests.
If you still want to _advertise_ them in lowercase, then I think the workarounds suggested above are sufficient options.
This was apparently changed/fixed in ApiExplorer in https://github.com/aspnet/Mvc/issues/8006
Most helpful comment
I was running into the same issue using dotnetcore 2.1.
I wouldn't doubt that there is a better way, but this worked for me.