ASP.NET MVC: ChildActionOnly action filter
ChildActionOnly action filter confirms that specified action method should be only accessible by child request. Unlike NonAction filter, such methods are still treated as action method but it can’t be invoke as a parent request. More specific user can’t invoke it direct by requesting action name but we developer can invoke it through code.
Attribute Usage: Controller & method
Sample Code:
[ChildActionOnly]
public ActionResult About()
{
return View();
}
We can also apply this action filter at controller level so all action method of specified controller will be accessible by only child request.
What is child request in ASP.NET MVC?
So now next question arise is what is child request? So child request means we can’t access it by direct url in other word we can’t use it to begin new request/response pipeline but we can use it as action method from within code where request/response pipeline are already initiated by some other request or action method. We can use such child action method as follow.
@Html.Action("ChildActionMethod")
@{
Html.RenderAction("ChildActionMethod");
}
If we try to access such method with direct url then it will throw an System.InvalidOperationException.
Check out ASP.NET MVC: Action filter series post to read about other available action filters.
You can follow me on twitter for latest link and update on ASP.NET & MVC.