Skip to content

Attributes implementing IRouteTemplateProvider should only be allowed on public methods #56791

Open
@m-gallesio

Description

@m-gallesio

Background and Motivation

As far as I am aware, routable controller methods need to be public.
However, there seems to be no rule against applying route attributes (e.g. [Route], [AcceptVerbs], ..., possibly all inheritors of IRouteTemplateProvider) to non-public methods.

I just accidentally left a method private and wondered why its route would not be mapped before thinking about checking its access modifiers.
This can happen when extracting methods via automated IDE refactorings (in Visual Studio the extracted method is set to private by default).

Proposed Analyzer

Analyzer Behavior and Message

The analyzer should notify when a route attribute is applied to an invalid method:

  • non-public methods
  • methods marked with [NonAction] (ideally considering inheritance)

This should apply to [Route], [AcceptVerbs], [HttpGet] and the like. This might coincide with inheritors of IRouteTemplateProvider (I do not know the internals of the framework enough to say).

Category

  • Design
  • Documentation
  • Globalization
  • Interoperability
  • Maintainability
  • Naming
  • Performance
  • Reliability
  • Security
  • Style
  • Usage

Severity Level

  • Error
  • Warning
  • Info
  • Hidden

Usage Scenarios

[HttpGet]
[Route("myRoute")]
private IActionResult SomeMethod() { /* ... */ }

Proposed corrections:

  • make the method public
  • remove the inapplicable attributes

Risks

This might break some non-explicit ways of declaring routes I am not aware of.

Metadata

Metadata

Assignees

No one assigned

    Labels

    analyzerIndicates an issue which is related to analyzer experienceapi-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions