1
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.

2 Answers 2

2

Restating what is written here:

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 no, the Controller suffix is not mandatory in ASP.NET Core. Only one of the above conditions need to be true.

Though I would suggest using the suffix because it will make your life easier browsing your solution.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Well, yes you are partially correct its a naming convernsion tough. However, In ASP.NET Core, while it is not mandatory for a controller class to have the suffix "Controller," but it is highly recommended to follow this convention. According to the official documentation from Microsoft you can refer here.

If there are any potential consequences for not following it. Even though my .NET 6 application seems to function without issue currently.

In general there wouldn't be any issue but if you use few advance level feature like reflection, you might encounter an issue.

Because, the framework uses reflection to discover controller classes. While it can find classes without the "Controller" suffix, using the suffix helps the framework easily identify and differentiate controller classes from other classes. If the suffix is omitted, you need to explicitly mark the class with the [Controller] attribute.

By default, ASP.NET Core searches for classes ending with "Controller" when setting up the MVC middleware.

Apart from that, convention-based routing, filters, and dependency injection, might not work as expected if the controller naming conventions are not followed.

Additionally, if you use scaffolding feature, so scaffolding usually serach for "Controller" suffix while generating views and other files for you may potentially leading to issues in automation and code generation.

Nonetheless, while your current setup can work, it is generally advisable to adhere to the naming convention of appending "Controller" to your controller classes. This helps avoid potential issues and aligns your code with common practices in the ASP.NET Core community. So, it is recommended to rename your class to HomeController for consistency and clarity.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.