asp.net - ASP.NET ActionFilter 的生命周期

标签 asp.net action-filter

我正在使用自定义 ActionFilterAttribute 在 ASP.NET 应用程序中执行一些日志记录。我正在添加收入信息(通过 OnActionExecuting)和传出信息(通过 OnActionExecuted)的日志记录。

我们有一个调用 token (GUID),我可以在OnActionExecuting中访问它,并且它是正在记录的信息的一部分。我希望在 OnActionExecuted 中完成的日志记录中使用相同的 token 。

我的问题:

  1. 是否为每个来电创建一个 ActionFilterAttribute 实例?
  2. 这是用于 OnActionExecutingOnActionExecuted 的同一个实例(允许我将 token 存储为成员变量)吗?

最佳答案

由于没有人回复,并且我通过自己的测试得到了答案,因此我想在这里发布答案。

事实证明,操作过滤器的实例被创建并在各种 Controller 操作调用之间共享。因此,使用成员变量在请求(在 OnActionExecuting 中设置它)和响应(在 OnActionExecuted 中读取它)之间共享公共(public)数据并不是完成我的任务的可靠方法。有意为之。

关于asp.net - ASP.NET ActionFilter 的生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63745793/

相关文章:

asp.net - 错误 Microsoft.Web.Infrastruct,版本 = 1.0.0.0,文化 = 中性,PublicKeyToken = 31bf3856ad364e35

c# - 如何在 NopCommerce 中实现 Action 过滤器

c# - 无法使用 Simple Injector 注册 Api Controller?

asp.net - 有没有办法将 Thickbox 与动态内容一起使用?

.net - 是什么导致错误 "Collection was modified after the enumerator was instantiated"?

asp.net-mvc-2 - 如何在 ASP.NET MVC 中将动态变量传递给 Action Filter

asp.net-mvc - 错误 : "This webpage has a redirect loop" in asp. 网络 mvc

c# - ASP.NET MVC : How to create an action filter to output JSON?

asp.net-mvc - ActionFilter 和 ResultFilter 的不同和示例

asp.net - 在 ASP.NET 中使用外部 CSS 文件显示背景图像时出现问题