-
Notifications
You must be signed in to change notification settings - Fork 711
Microsoft.AspNetCore.Mvc.Versioning 5.0.0 removes number from end of controller name #734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
In short, this has always been the case since ASP.NET has a built-in convention to strip off the trailing It should be noted that route template and controller name are distinct things. Regardless of version, all related controllers should have the same logical name or they cannot be grouped together. This may not necessarily flow into your route template. You did not show the code or route template, but I'm guessing it is defined as
All versions of the Weather Forecast service should have the same path regardless of version. |
That doesn't seem to be the case. Going through the reproduction steps and installing Note that I'm not arguing that the new behavior is incorrect, just that it's breaking and should be documented as such.
Yes, that is what the default template included with Visual Studio uses. (For completeness, we did just remove the |
I agree with @kimsey0 it' breaking change |
This is a regression so I'll accept it as a bug. The behavior that I've already described is what has always happened and will not be changing. What changed is from some recent performance optimizations. The trimming was happening on every use of This setup is very strange to me. Why would anyone want to do this? It effectively adds its own pseudo mechanism for versioning in the path. If you're going to do it that way, then you probably don't need API Versioning. Controllers will still be grouped by You didn't show the controller or any code for that matter. Based on the repro steps, you haven't added any attributes or conventions and used all the default settings. This means that As a workaround, I would expect the following route template to work: [Route("WeatherForecastV{version:apiVersion}")] This can be made to pair with the API Explorer extensions to produce the correct URL. |
I'm not going to try. We have fixed the one weird case where it affected us. I was just surprised that having a number at the end of a controller name led to ApiExplorer (or the interaction with Swashbuckle?) generating incorrect API paths. I could imagine the same surprise if the number had nothing to do with versioning. (Maybe
Yes. The reproduction steps in the report produce a full and working example that demonstrates the regression. I did not need to add any additional attributes or conventions. If you don't have access to an environment with Visual Studio, or find that there's too much code in the template solution for it to be a minimal working example, I can try and create a smaller one? |
I'm going to have to think about what the long-term solution for this is. The original intent was to ease the requirements for code organization. .NET types must be unique per namespace. The original behavior addresses the common scenario if you chose to have all of your controllers in a single namespace. Consider the following:
All 3 of these should have the controller name and route parameter value It hasn't happened often, but this is not the first time that people have been hung up by this magical convention. People expect:
To work by default, but it doesn't. ASP.NET will not strip the suffix because it is not exactly I can certainly add the hook with built-in behaviors to control how this is processed, but I'm pretty sure most will not understand this is what is causing them grief and that they need to change things. Discoverability of this nuance is difficult no matter which direction you come at it from. I'm open to suggestions. |
I'm not sure that anything needs to change. I understand the reasoning behind stripping the numbers which you describe above. If I actually wanted my (fictional) But I would at least document the change in behavior. The release may have been out for a couple of months now, but there are probably going to be developers upgrading for a long time. (They may be staying on .NET Core 3.1 LTS until .NET 6.) Maybe they can search up this issue, but it might be easier to find if it was linked from https://github.com/microsoft/aspnet-api-versioning/releases/tag/v5.0.0 |
Fair enough. As I seem to recall now, I realized things (from the other side) didn't work properly if you didn't explicitly add the controller name or route template. Defining the route template with the expected path or ControllerNameAttribute will take precedence over the convention. I'll add a section to the wiki about this and link it back to the release notes. |
A small update on this issue. I've decided to lift this behavior out into the new: interface IControllerNameConvention
{
string NormalizeName(string controllerName);
string GroupName(string controllerName);
} There will be 3 out-of-the-box variants:
Changing the behavior (like you want) will look like: options.ControllerNameConvention = ControllerNameConvention.Grouped; // instead of '.Default' For any other scenario or edge case, people can roll their own implementation. No commitment on a specific release date, but I've got things put together and it will be available in the next iteration |
I have a controller for AWS S3 and the controller name ending with S3, suddenly I found all my request returned 404, it wasted me a few hours before I found this thread... |
Hi @commonsensesoftware |
@nieuwda this was fixed in the Explicit route templates (e.g. without the |
Microsoft.AspNetCore.Mvc.Versioning 5.0.0 seems to introduce a breaking change from 4.2.0 where it removes numbers at the end of controller names when generating API descriptions and paths for them.
Reproduction steps:
WeatherForecastController
class toWeatherForecastV1Controller
.Microsoft.AspNetCore.Mvc.Versioning
NuGet package in version 5.0.0.services.AddApiVersioning();
afterservices.AddControllers();
inStartup.cs
.Expected results:
The Swagger UI shows one path,

WeatherForecastV1
, and allows calling it at `/WeatherForecastV1, which is correctly routed to the controller by ASP.NET Core (though returning a 400 status code).This behavior is seen if you install version 4.2.0 of the NuGet package above.
Actual results:
The Swagger UI shows one path,

WeatherForecastV
, and calls it at/WeatherForecastV
, which is not correctly routed toWeatherForecastV1Controller
and therefore returns a 404 status code.The text was updated successfully, but these errors were encountered: