Skip to content

Commit

Permalink
granner
Browse files Browse the repository at this point in the history
  • Loading branch information
mary-georgiou-sonarsource committed Feb 23, 2024
1 parent 2a2ce59 commit f0c3861
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions rules/S6934/csharp/rule.adoc
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
The https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing[routing] middleware in https://learn.microsoft.com/en-us/aspnet/core/mvc/overview[ASP.NET Core MVC] uses a set of predefined rules and conventions to determine which controller and action method to invoke for a given HTTP request. This is typically defined with the https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.controllerendpointroutebuilderextensions.mapcontrollerroute[`MapControllerRoute`] method during the application configuration.
However, without some extra configuration on the developer's part sometimes the routing system is not able to properly resolve a route and map it to a certain action resulting in unexpected behavior or errors.
The https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing[routing] middleware in https://learn.microsoft.com/en-us/aspnet/core/mvc/overview[ASP.NET Core MVC] uses a set of predefined rules and conventions to determine which controller and action method to invoke for a given HTTP request. The routing configuration is typically defined with the https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.controllerendpointroutebuilderextensions.mapcontrollerroute[`MapControllerRoute`] method during the application configuration.
However, without some extra configuration on the developer's part, sometimes the routing system cannot correctly resolve a route and map it to a certain action, resulting in unexpected behavior or errors.

== Why is this an issue?

In ASP.NET MVC, when a https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.routing.httpmethodattribute[`HttpMethodAttribute`] (such as https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.httpgetattribute[`HttpGet`], https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.httppostattribute[`HttpPost`], etc) is specified with a given route template at the action level, it's important that its controller also has a https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.routeattribute[`RouteAttribute`] defined. If not then the route pattern defined `WebApplication.MapControllerRoute` is applied resulting in a not expected route and potential confusion. This applies also to when https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas[`areas`] are defined.
In ASP.NET MVC, when a https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.routing.httpmethodattribute[`HttpMethodAttribute`] (such as https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.httpgetattribute[`HttpGet`], https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.httppostattribute[`HttpPost`], etc) is specified with a given route template at the action level, it's important that its controller also has a https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.routeattribute[`RouteAttribute`] defined. If not, then the route pattern that has been defined in `WebApplication.MapControllerRoute` is applied, resulting in an unexpected route and potential confusion. This applies also to when https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/areas[`areas`] are defined.


== How to fix it

When any of the controller actions is annotated with a `HttpMethodAttribute' with a route template, then you should also annotate the controller with the `RouteAttribute` as well.
When any of the controller actions is annotated with a `HttpMethodAttribute' with a route template, you should also annotate the controller with the `RouteAttribute` as well.

=== Code examples

Expand Down

0 comments on commit f0c3861

Please sign in to comment.