Using Childactiononly in MVC

Using ChildActionOnly 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.
Having defined an action method, we need to create what will be rendered when the action is
invoked. Child actions are typically associated with partial views, although this is not compulsory.

  1. [ChildActionOnly] allowing restricted access via code in View

  2. State Information implementation for specific page URL.
    Example: Payment Page URL (paying only once)
    razor syntax allows to call specific actions conditional

Using Ajax.ActionLink and ChildActionOnly together in MVC fails

ChildActionOnlyAttribute can be used only with the HTML extension methods.

The ChildActionOnly attribute ensures that an action method can be called only as a child method from within a view. we tend to use this attribute to prevent the action methods from being invoked as a result of a user request.

In your case,instead of using [ChildActionOnly] attribute use AjaxOnly as shown :

[AjaxOnly]
public PartialViewResult Information()

here is how you can make one

public class AjaxOnlyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(!filterContext.HttpContext.Request.IsAjaxRequest())
filterContext.HttpContext.Response.Redirect("/error/404");
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{

}
}

and use it like

[AjaxOnly]
public PartialViewResult Information()

How to call MVC ChildActionOnly Controller Action using jQuery

No, you cant do that. The whole point of the ChildActionOnly attribute is to prevent it being invoked as a result of a user request.

ASP.NET - What's the difference between ChildActionOnly and NonAction attributes?

ChildActionOnly -- can only be called by another action and not directly from an external call (via routing). Permitted actions include Action/RenderAction extension methods.

NonAction -- Like marking a method "private" in regards to keeping it from being accessible from either an external call or as a child action. Good for protecting actions you don't need/want created as a direct view. Worth mentioning this is only necessary on public methods (as private/protected aren't considered "actionable").

See also:

  • Using ChildActionOnly in MVC
  • ASP .NET MVC NonAction meaning

Net Core 2 equivalent of ChildActionOnly

There is no ChildActionOnly equivalent in ASP.NET Core. Use view components instead. Also see different blogs related to this.

Partial View With ChildActionOnly Attribute issue

you can use AjaxOnly

[AjaxOnly]
[HttpPost]
public ActionResult LeftColumnData()
{
var Column= new ColumnViewModel { Attempt= DateTime.Now };
return PartialView("_LeftColumn", Column);
}

here is how you can make one

public class AjaxOnlyAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if(!filterContext.HttpContext.Request.IsAjaxRequest())
filterContext.HttpContext.Response.Redirect("/error/404");
}

public override void OnActionExecuted(ActionExecutedContext filterContext)
{

}
}

and use it like

[AjaxOnly]
public ActionResult AjaxActionMethod()
{
....
}


Related Topics



Leave a reply



Submit