Skip to content

Commit

Permalink
Implement exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
costin-zaharia-sonarsource committed Mar 21, 2024
1 parent 234510c commit b6b1da3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override void Initialize(SonarAnalysisContext context) =>
&& method.GetAttributes().Any(x =>
x.AttributeClass.DerivesFromAny(RouteTemplateAttributes)
&& x.TryGetAttributeValue<string>("template", out var template)
&& !string.IsNullOrWhiteSpace(template)))
&& !CanBeIgnored(template)))
{
secondaryLocations.Push(node.Identifier.GetLocation());
}
Expand All @@ -82,4 +82,11 @@ private static void ReportIssues(SonarSymbolReportingContext context, ISymbol sy
}
}
}

private static bool CanBeIgnored(string template) =>
string.IsNullOrWhiteSpace(template)
// See: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing#combining-attribute-routes
// Route templates applied to an action that begin with / or ~/ don't get combined with route templates applied to the controller.
|| template.StartsWith("/")
|| template.StartsWith("~/");
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public class RouteTemplateIsNotSpecifiedController : Controller
[method: HttpGet]
public IActionResult Index7() => View(); // Compliant

[HttpGet("/[controller]/[action]/{sortBy}")]
public IActionResult AbsoluteUri1(string sortBy) => View(); // Compliant, absolute uri

[HttpGet("~/[controller]/[action]/{sortBy}")]
public IActionResult AbsoluteUri2(string sortBy) => View(); // Compliant, absolute uri

public IActionResult Error() => View(); // Compliant
}

Expand Down

0 comments on commit b6b1da3

Please sign in to comment.