Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...