Controller class name should be suffixed with 'Controller'
Warns about ASP.NET Controller classes not suffixed with Controller, a recommended practice: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/actions ControllerBase is also accepted for base classes.
How to fix
Suffix the names of matched Controller classes with Controller.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Controller class name should be suffixed with 'Controller'</Name>
// <Id>ND2021:ControllerClassNameShouldBeSuffixedController </Id>
warnif count > 0 from t in Application.Types where
(t.DeriveFrom("Microsoft.AspNetCore.Mvc.Controller".AllowNoMatch()) ||
t.DeriveFrom("Microsoft.AspNetCore.Mvc.ControllerBase".AllowNoMatch()) ||
t.HasAttribute("Microsoft.AspNetCore.Mvc.ControllerAttribute".AllowNoMatch()))
&&
// We use SimpleName, because in case of generic Exception type
// SimpleName suppresses the generic suffix (like <T>).
!t.SimpleNameLike(@"Controller$") &&
!t.SimpleNameLike(@"ControllerBase$") // Allow the second suffix Base
// for base Controller classes.
select new {
t,
Debt = 5.ToMinutes().ToDebt(),
Severity = Severity.High
}
//<Expl>
// The *controller {0}'s name* should be suffixed with *Controller*.
// This is required in the *Microsoft* documentation.
//</Expl>
//<Desc>
// Warns about *ASP.NET Controller classes* not suffixed with
// **Controller**, a recommended practice:
// https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/actions
// **ControllerBase** is also accepted for base classes.
//</Desc>
//<HowToFix>
// Suffix the names of matched Controller classes with **Controller**.
//</HowToFix>
Open in the interactive Rules Explorer · All default rules
Naming Conventions: all 23 items
- Instance fields naming convention ND2000
- Static fields naming convention ND2001
- Interface name should begin with a 'I' ND2002
- Abstract base class should be suffixed with 'Base' ND2003
- Exception class name should be suffixed with 'Exception' ND2004
- Attribute class name should be suffixed with 'Attribute' ND2005
- Types name should begin with an Upper character ND2006
- Methods name should begin with an Upper character ND2007
- Do not name enum values 'Reserved' ND2008
- Avoid types with name too long ND2009
- Avoid methods with name too long ND2010
- Avoid fields with name too long ND2011
- Avoid having different types with same name ND2012
- Avoid prefixing type name with parent namespace name ND2013
- Avoid naming types and namespaces with the same identifier ND2014
- Don't call your method Dispose ND2015
- Methods prefixed with 'Try' should return a boolean ND2016
- Properties and fields that represent a collection of items should be named Items. ND2017
- DDD ubiquitous language check ND2018
- Avoid fields with same name in class hierarchy ND2019
- Avoid various capitalizations for method name ND2020
- Controller class name should be suffixed with 'Controller' ND2021
- Nested class members should not mask outer class' static members ND2022