3

Just out of curiosity, what's the difference between these two attributes?

NonAction and ChildActionOnly

Both attributes seems to do the very same to me. Preventing the pipeline (action invoker) to invoke the action method. Please have a look at this snippet:

[NonAction]
private ActionResult StackOverflow1()
{
    // Omitted for brevity.
    return View();
}

[ChildActionOnly]
private ActionResult StackOverflow2()
{
    // Omitted for brevity.
    return View();
}

Of course both methods won't be invoked due to the fact that they are private - at least at the default implementation - but I would like to know the difference just out of curiosity...

Baccata
  • 473
  • 1
  • 7
  • 15

1 Answers1

5

ChildActionOnly -- can only be called by another action and not directly from an external call (via routing). Permitted actions include Action/RenderAction extension methods.

NonAction -- Like marking a method "private" in regards to keeping it from being accessible from either an external call or as a child action. Good for protecting actions you don't need/want created as a direct view. Worth mentioning this is only necessary on public methods (as private/protected aren't considered "actionable").

See also:

Community
  • 1
  • 1
Brad Christie
  • 100,477
  • 16
  • 156
  • 200