ASP.NET Core MVC Interview Question #31

What is ActionNameAttribute in ASP.NET Core MVC?

Controllers, Actions, Model Binding & Action Results Junior Beginner

Quick Interview Answer

ActionNameAttribute gives an action a different externally visible action name from its C# method name. This can be useful when two methods need distinct C# names but should participate in routing under a desired action name.

Detailed Explanation

ActionNameAttribute gives an action a different externally visible action name from its C# method name. This can be useful when two methods need distinct C# names but should participate in routing under a desired action name.

Use it sparingly because excessive renaming can make controller code harder to follow.

Code Example

[HttpPost]
[ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
    // Delete item
    return RedirectToAction(nameof(Index));
}