Skip to content

Changes in MVC controller discovery #165

@javiercn

Description

@javiercn

We have introduced some changes to simplify the way we do controller discovery. These changes are:

  • There is a new Controller attribute that can be used to mark a class and their descendants
    as controllers.
  • Classes whose name doesn't end in Controller and derive from a base class that ends in Controller
    are no longer considered controllers. In this scenario the Controller attribute must be applied
    to the controller class itself or to the base class.

We now consider a type to be a controller if all of the following rules apply:

  • The type is a public, concrete, non open generic class.
  • [NonController] is not applied to any type of the hierarchy.
  • The type name ends with controller or [Controller] is applied to the type or to one of its ancestors.

It's important to note that if [NonController] is applied anywhere in the type hierarchy the discovery
conventions will never consider that type or its descendants to be a controller. [NonController]
always wins over [Controller].

Here are some samples of what is considered a controller with the new conventions:

Poco controller (ends with controller):

public class PocoController { } // controller name: Poco

Class extending Microsoft.AspNetCore.Mvc.Controller or Microsoft.AspNetCore.Mvc.ControllerBase (ControllerBase has Controller attribute applied to it):

public class Products : Controller { } // controller name: Products
[Controller]
public abstract class MyControllerBase { }

public class Products : MyControllerBase { } // controller name: Products

For more details you can see the discussion thread on aspnet/Mvc#3732

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions