在ASP.NET MVC 3,什么是filterContext.IsChildAction? [英] In ASP.NET MVC 3, what is filterContext.IsChildAction?

查看:314
本文介绍了在ASP.NET MVC 3,什么是filterContext.IsChildAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从它的声音,它确实是的动作是否是一个孩子的动作的布尔值。

From the sounds of it, it literally is a boolean value of whether or not the action is a child action.

我看到code这一点往往

I see this bit of code quite often:

protected override void OnActionExecuting(ActionExecutingContext filterContext) {
    if (filterContext.IsChildAction) return;
    ...
}

这似乎是在那里节流不必要code执行......但到底是什么filterContext.IsChildAction实际上意味着什么呢?

It appears to be there to "throttle" unnecessary code execution... but what does filterContext.IsChildAction actually mean?

推荐答案

在视图页面中,您可能经常需要的另一个动作输出注入到当前页面 - 例如,注射菜单。菜单生成可涉及大量的业务逻辑(确定权或用户,选择选定的项目,等等),因此它不会在局部视图进行,但在控制器。

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);
   }
}

这类型的操作被称为ChildAction,因为它不能(不应该)被称为来自外界的(通过访问URL)。这只能由应用程序本身来调用,一般从视图页面中。

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")

如果你希望(或不希望)做执行操作时的一些具体的东西是一个孩子的动作,你检查 filterContext.IsChildAction 属性。

这篇关于在ASP.NET MVC 3,什么是filterContext.IsChildAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆