Is controller suffix is mandatory in .NET Core (.NET 6)

Amit 711 Reputation points
2024-06-18T16:11:45.6+00:00
public class Home : controller
{
	public IActionResult Index()
	{
		return view();
	}
}

I created above class without controller suffix.

And below is my routing in startup.cs class .NET 6:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}"
       );
});

I just wanted to confirm whether appending "Controller" as a suffix is simply a naming convention, or if there are any potential consequences for not following it. Even though my .NET 6 application seems to function without issue currently.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,344 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ping Ni-MSFT 3,115 Reputation points Microsoft Vendor
    2024-06-19T06:50:11.06+00:00

    Hi @Amit,

    I just wanted to confirm whether appending "Controller" as a suffix is simply a naming convention, or if there are any potential consequences for not following it. Even though my .NET 6 application seems to function without issue currently.

    You can check the official document to know what is a controller. A controller is an instantiable class, usually public, in which at least one of the following conditions is true:

    • The class name is suffixed with Controller.
    • The class inherits from a class whose name is suffixed with Controller.
    • The [Controller] attribute is applied to the class.

    So, there is no potential consequences for do not add the suffix if the class is inherits from Controller .


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Rena

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 60,361 Reputation points
    2024-06-18T20:29:40.5933333+00:00

    The "Controller" suffix is only required if the class does not inherit from the Controller base class.

    2 people found this answer helpful.
    0 comments No comments