• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

1、配置log4net查下以下链接

https://www.cnblogs.com/handsomeziff/p/16213689.html

2、添加类CustomActionFilterAttribute,继承属性,IActionFilter

ContractedBlock.gif

ExpandedBlockStart.gif

一使用微软AspNetCore。Mvc。过滤

2

3命名空间项目6。实用过滤器

4 {

5公共类CustomActionFilterAttribute :属性,IActionFilter

6 {

7 private readonly ILoggerCustomActionFilterAttribute _ ILogger;

8

9公共CustomActionFilterAttribute(ILoggerCustomActionFilterAttribute iLogger)

10 {

11这个ILogger=iLogger。

12 }

13

14 ///摘要

15 ///在行动之后执行

16////摘要

17 ///param name='context'/param

18公共void on action executed context上下文)

19 {

20瓦结果=牛顿软件.JSON。JSON转换。序列化对象(上下文。结果);

21变量控制器名称=上下文HttpContext。GetRouteValue(' controller ');

22 var actionName=context .HttpContext。GetRouteValue(' action ');

23 _伊罗格.逻辑信息($ '执行{控制器名}-{操作名},结果为:{ result } ');

24 }

25

26 ///摘要

27 ///在行动之前执行

28////摘要

29 ///param name='context'/param

30 public void on actionexecuting(ActionExecutingContext上下文)

31 {

32 var para=上下文HttpContext。请求。查询字符串。价值;

33 var控制器名称=上下文HttpContext。GetRouteValue(' controller ');

34 var actionName=context .HttpContext。GetRouteValue(' action ');

35 _伊罗格.逻辑信息($ '执行{控制器名}-{操作名},参数为:{ para } ');

36 }

37 }

38 }

查看代码

3、添加控制器CustomActionFilterController

ContractedBlock.gif

ExpandedBlockStart.gif

一使用微软AspNetCore。手动音量调节

2使用项目6 .实用过滤器;

3

四命名空间项目6。控制器

5 {

6公共类CustomActionFilterController :控制器

7 {

8[类型过滤器(CustomActionFilterAttribute的类型))]

9//[服务过滤器(类型为(CustomActionFilterAttribute))]

10公共IActionResult索引(int id)

11 {

12 ViewBag.user=Newtonsoft .JSON。JSON转换。序列化对象(新

13 {

14 Id=id,

15 Name='ViewBag '

16 });

17查看数据[' UserInfo ']=牛顿软件.JSON。JSON转换。序列化对象(新

18 {

19 Id=id 1,

20 Name='ViewData '

21 });

22

23对象描述='描述信息;

24返回视图(描述);

25 }

26 }

27 }

查看代码

4、在各个方法处设置断点,运行程序CustomActionFilter?id=1

可以发现程序先执行CustomActionFilterAttribute构造函数-

控制器构造函数-

行动中执行方法-

控制器里面的索引方法-

行动中已执行方法

所以行动中执行和行动中已执行分别是在控制器方法执行前后执行的。可以用于请求前后的日志记录。

Link to comment
Share on other sites