Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ASP.NET MVC 3, what is filterContext.IsChildAction?

Tags:

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?

like image 344
Chaddeus Avatar asked Nov 09 '11 06:11

Chaddeus


People also ask

What is meaning of @: In .NET MVC?

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.

What is ChildActionOnly attribute in MVC?

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.

What is the use of ActionName attribute in MVC?

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.

What is MVC in asp net with example?

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.


2 Answers

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.

like image 74
archil Avatar answered Jan 04 '23 00:01

archil


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

  1. as a regular action

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

like image 24
Souhaieb Besbes Avatar answered Jan 04 '23 01:01

Souhaieb Besbes