Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to automatically ignore childaction in .net mvc #554

Closed
hlhxwithmj opened this issue Dec 9, 2022 · 10 comments
Closed

how to automatically ignore childaction in .net mvc #554

hlhxwithmj opened this issue Dec 9, 2022 · 10 comments

Comments

@hlhxwithmj
Copy link

Describe the bug
If I call @Html.Action("GridView") as a child action to render dynamic content in my razor view, AuditAction.ActionName will be replaced with child action name not the real action name like "Index" thing.
Is this a bug or something? By labeling "GridView" child action as AuditIgnore

Expected behavior
Ignore child action automatically.

Libraries (specify the Audit.NET extensions being used including version):
For example:

  • Audit.NET.20.1.0
  • Audit.Mvc.20.1.0

Target .NET framework:

  • .NET Framework 4.5.1
@thepirat000
Copy link
Owner

Are you using MVC 5?
How are you setting up the audit in the controller/page model? can you share the code?

@hlhxwithmj
Copy link
Author

Are you using MVC 5? How are you setting up the audit in the controller/page model? can you share the code?

Yes, I'm using MVC5.
In DemoController.cs:
{
public ActionResult Index(){}
public PartialViewResult MyGridViewPartial(){}
}
In Demo/Index.cstml
{
@Html.Action("MyGridViewPartial")
}
In browser, go to "http://localhost/Demo".
Then, I've got an output like ControllerName "Demo", ActionName "MyGridViewPartial".

@thepirat000
Copy link
Owner

thepirat000 commented Dec 12, 2022

Yes, it's a bug when you set up the AuditAttribute filter globally (or at the controller level) and ignore some of the controller methods (used as child actions) with AuditIgnore.

The MVC 5 engine will call the Action Filter method OnResultExecuted() in a way that is not possible to get the originating method (and therefore there is no reliable way to know if the originating method was ignored with [AuditIgnore]).

You can see the issue described here:

A workaround could be to mark with [Audit] only those action methods that need auditing.

I still need to think a little bit more about this, but I guess another solution could be to provide a new configuration parameter to the attribute like [Audit(IncludeChildActions = false)] to allow ignoring the child actions.
With this, you could set the attribute to the entire controller (or globally) and it will not log any results from the child actions.

In the meantime, the same effect can be achieved by creating a custom attribute that inherits from AuditAttribute and avoid the auditing by checking the IsChildAction property:

public class AuditNoChildAttribute : AuditAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        if (!filterContext.IsChildAction)
            base.OnActionExecuting(filterContext);
    }
    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        if (!filterContext.IsChildAction)
            base.OnActionExecuted(filterContext);
    }
    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if(!filterContext.IsChildAction)
            base.OnResultExecuted(filterContext);
    }
}

So then you can use [AuditNoChild(...)] instead of [Audit()]

thepirat000 added a commit that referenced this issue Dec 12, 2022
@thepirat000
Copy link
Owner

IncludeChildActions was added on version 20.1.1, please upgrade your references and re-test

[Audit(IncludeChildActions = false)]
public class YourController : Controller
{
}

@hlhxwithmj
Copy link
Author

Thanks, and it looks working great!

@hlhxwithmj
Copy link
Author

This solution works fine when I get response from action like "Index". But it will be not that right when I call "POST" request from my partial view to refresh part of data even if having set the attribute on controller.

@hlhxwithmj hlhxwithmj reopened this Dec 13, 2022
@thepirat000
Copy link
Owner

But AuditIgnore should work in that case, right?

@hlhxwithmj
Copy link
Author

But AuditIgnore should work in that case, right?

Yes, absolutely.

@thepirat000
Copy link
Owner

Did you try with AuditIgnore and is not working?

@hlhxwithmj
Copy link
Author

AuditIgnore works well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants