ASP.NET MVC NonActionAttribute使用说明

默认情况下,MVC 框架将 controller 类的所有公共方法都视为操作方法Action(浏览器输入地址即可访问)。

 如果您的 controller 类包含公共方法,并且您不希望它成为操作方法Aciton,则必须用 NonActionAttribute 特性标记该方法。

下面的示例演示用 NonAction 特性标记的方法。

NonActionAttribute 类

表示一个特性,该特性用于指示控制器方法不是操作方法。在其他控制器中可调用该方法

[NonAction]
        public string GetClassName()
        {
            return "hello";
        }

//controller 中直接调用GetClassName()

ChildActionOnly 类

ChildActionOnly表示它只能在View中通过Html.Action或Html.RenderAction来使用,不能被 Controller 直接调用, 一般返回的是局部视图,例如更新局部页面时,在 主view 中使用 Html.Action 或 Html.RenderAction 来调用

[ChildActionOnly]
        public ActionResult GetClassName(int id)
        {
            return Content("体育");
        }

//view中直接调用@Html.Action("GetClassName", new { id = 1 })


----------end-----------

原文地址:https://www.cnblogs.com/webapi/p/5669042.html