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.
[ChildActionOnly] allowing restricted access via code in View
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
Performance Tests of Serializations Used by Wcf Bindings
Drag and Drop to Desktop/Explorer
Could Not Load File or Assembly System.Runtime.Compilerservices.Unsafe
Convert Arabic"Unicode" Content HTML or Xml to PDF Using Itextsharp
ASP.NET MVC Razor: How to Render a Razor Partial View's HTML Inside the Controller Action
Converting Svg to Png Using C#
Set Gridview Backcolor to Color from Datatable
For VS. Linq - Performance VS. Future
SQL Error: Incorrect Syntax Near the Keyword 'User'
Dbentityvalidationexception - How to Easily Tell What Caused the Error
How to Use the Iequalitycomparer
Is There Really Any Way to Uniquely Identify Any Computer at All
C# Reflection - Get Field Values from a Simple Class
Variable Does Not Exist in the Current Context
How to Update the Value Stored in Dictionary in C#
How to Get the CPU Temperature