rspec/rules/S6931/why.adoc

24 lines
2.1 KiB
Plaintext

== Why is this an issue?
https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing[Routing] in ASP.NET Core MVC maps https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/actions#what-is-a-controller[controllers] and https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/actions#defining-actions[actions] to paths in request https://en.wikipedia.org/wiki/Uniform_Resource_Identifier[URIs]. Similar https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs[routing] happens in ASP.NET Framework MVC.
In ASP.NET Core MVC, when an action defines a route template starting with a "/", the route is considered absolute and the action is registered at the root of the web application.
In such a scenario, any route defined at the controller level is disregarded, as shown in the following example:
include::{language}/why-example.adoc[]
The behavior can be found confusing and surprising because any relative action route is relativized to the controller route.
Therefore, in the vast majority of scenarios, controllers group all related actions not only in the source code, but also at the routing level.
In ASP.NET Framework MVC with attribute routing enabled via https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.routecollectionattributeroutingextensions.mapmvcattributeroutes[`MapMvcAttributeRoutes`], the mere presence of an absolute route at the action level will produce an `InvalidOperationException` at runtime.
It is then a good practice to avoid absolute routing at the action level and move the "/" to the root level, changing the template defined in the `RouteAttribute` of the controller appropriately.
=== Exceptions
The rule only applies when all route templates of all actions of the controller start with "/".
Sometimes some actions may have both relative and absolute route templates, for example for backward compatibility reasons (i.e. a former route needs to be preserved).
In such scenarios, it may make sense to keep the absolute route template at the action level.