From the sounds of it, it literally is a boolean value of whether or not the action is a child action.
I see this bit of code quite often:
protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.IsChildAction) return; ... }
It appears to be there to "throttle" unnecessary code execution... but what does filterContext.IsChildAction actually mean?
Using @: to explicitly indicate the start of content We are using the @: character sequence to explicitly indicate that this line within our code block should be treated as content.
The ChildActionOnly attribute ensures that an action method can be called only as a child method from within a view. An action method doesn't need to have this attribute to be used as a child action, but we tend to use this attribute to prevent the action methods from being invoked as a result of a user request.
ActionName attribute is an action selector which is used for a different name of the action method. We use ActionName attribute when we want that action method to be called with a different name instead of the actual name of the method.
The MVC convention is to put controllers in the Controllers folder that Visual Studio created when the project was set up. Let's take a look at a simple example of Controller by creating a new ASP.Net MVC project. Step 1 − Open the Visual Studio and click on File → New → Project menu option.
In view pages, you may often need to inject output of another action into current page - for example, injecting menus. Menu generation may involve lots of business logic (determining rights or users, choosing selected item, etc), so it is not done in the partial view, but in controller.
public class MenuController : Controller { [ChildActionOnly] public ActionResult Menu() { MenuViewModel model = GenerateMenu(); return View(model); } }
This type of action is called ChildAction, as it cannot(and is not supposed to) be called from outside world(by visiting url). This may only be called by application itself, generally from within the view page.
@Html.Action("Menu", "Menu")
And if you wish(or do not wish) to do some specific stuff when the action being executed is a child action, you inspect filterContext.IsChildAction
property.
Maybe it's too late to point out but the accepted answer is slightly misleading, in the sense that: action marked with ChildActionOnlyAttribute
absolutely cannot be run as standalone actions and so it is pointless to test with IsChildAction
.
On the other hand if your action is called in two ways
as a regular action
as a child action from within another action
It can be useful to check for IsChildAction
so that you can execute additional logic based on the value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With