【问题标题】:Audit.NET and Asp Net Core Razor PagesAudit.NET 和 Asp Net Core Razor 页面
【发布时间】:2020-12-21 06:03:59
【问题描述】:

我在我的 ASP.NET Core 应用程序中使用剃须刀页面。我需要使用Audit.NET library 启用日志记录,它适用于 ASP.NET MVC 控制器,但不适用于 Razor 页面。

这是一个示例,我如何使用 Audit 属性声明 PageModel 类:

[Audit(EventTypeName = "{area}/{Page} ({verb})",
   IncludeResponseBody = true,
   IncludeRequestBody = true,
   IncludeHeaders = true,
   IncludeModel = true)]
public class LoginIndexModel : PageModel
{
  ...
}

AuditAttribute 动作过滤器被调用时,它会抛出NullReferenceException

这是AuditAttribute中声明的方法:
(据我了解actionDescriptor 参数不能转换为ControllerActionDescriptor

private bool IsActionIgnored(ActionDescriptor actionDescriptor)
{
    if (actionDescriptor == null)
        return false;

    return ((IEnumerable<object>)(actionDescriptor as ControllerActionDescriptor).ControllerTypeInfo
               .GetCustomAttributes(typeof(AuditIgnoreAttribute), true)).Any<object>() || 
           ((IEnumerable<object>)(actionDescriptor as ControllerActionDescriptor).MethodInfo
               .GetCustomAttributes(typeof(AuditIgnoreAttribute), true)).Any<object>();
}

那么在这种情况下我该怎么办? 有没有人遇到过类似的问题?

【问题讨论】:

  • 这需要一些工作才能使其与 Razor Pages 兼容,我创建了 the following issue 来跟踪进度

标签: asp.net-core razor-pages audit.net


【解决方案1】:

MVC 的审计机制是通过操作过滤器实现的,但操作过滤器是not supported on Razor Pages

对于 razor 页面,提供了 Page Filter,因此您可以配置审核。

使用提供的AuditPageFilter 而不是[Audit] 属性。

基本上你只需要在你的启动逻辑上将过滤器添加到集合中,例如:

public void ConfigureServices(IServiceCollection services)
{
    services.AddRazorPages()
        .AddMvcOptions(options =>
        {
            options.Filters.Add(new Audit.Mvc.AuditPageFilter()
            {
                IncludeHeaders = true, ...
            });
        });
}

查看自述文件here

【讨论】:

    猜你喜欢
    • 2019-09-21
    • 2019-12-20
    • 2019-11-22
    • 2018-07-30
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    相关资源
    最近更新 更多