ASP.NET Core MVC Interview Question #30

What does the [NonAction] attribute do?

Controllers, Actions, Model Binding & Action Results Junior Beginner

Quick Interview Answer

NonAction tells MVC that a public method on a controller must not be treated as an action. It can be useful for public helper methods, although private or protected helper methods are often preferable when possible.

Detailed Explanation

NonAction tells MVC that a public method on a controller must not be treated as an action. It can be useful for public helper methods, although private or protected helper methods are often preferable when possible.

The attribute prevents action discovery for the marked method.

Code Example

[NonAction]
public string BuildTitle(string name)
{
    return $"Product: {name}";
}