netcore - MVC的ActionFilter的使用

经过一周的时间没有分享文章了,主要是在使用.netcore做一个小的项目,项目面向大众用户的增删改查都做的差不多了,打算本周在云服务器上部署试试,很期待,也希望上线后大家多多支持;以上纯属个人废话,来一起看看今天的正篇环节:

.继承IActionFilter来定义个验证登录的流程例子

.ActionFilter中怎么使用依赖注入

 

下面一步一个脚印的来分享:

.继承IActionFilter来定义个验证登录的流程例子

首先,咋们定义一个名叫FilterTestController的Controller,返回一个JsonResult结果;为了例子查看的方便性,同时在此文件中定义一个名MoResponse的类,对应的代码如下:

技术分享
 1 public class FilterTestController : Controller
 2     {
 3         public JsonResult Index()
 4         {
 5             var response = new MoResponse();
 6             response.Status = 1;
 7 
 8             return Json(response);
 9         }
10     }
11 
12     public class MoResponse
13     {
14 
15         public int Status { get; set; }
16         public string Des { get; set; } = "Ok";
17     }
View Code

然后,执行一下运行命令dotnet run,浏览器中输入默认端口好5000并访问刚才定义的Action,地址如:http://localhost:5000/FilterTes,不出意外大家都能看到如下结果的图样:

技术分享

这表示项目初建没有问题;

接着,创建一个类,名称为CheckLoginAttribute并且继承和实现Attribute, IActionFilter,这里实现IActionFilter接口的OnActionExecuting,OnActionExecuted两个方法,看方法名称后缀大概就明白一个是Action执行之前,一个是Action方法执行之后调用的,这里简单补充下以前的mvc版本大部分都是去继承ActionFilterAttribute,其实这个也去继承和实现了Attribute, IActionFilter,不过还有其他的扩展罢了,这里不详细区分说明:

技术分享

然后咋们分别在两个OnActionExecut中增加一些输入信息,并且在FilterTestController的Index方法上方增加[CheckLogin]标记,代码如下:

技术分享
public class CheckLoginAttribute : Attribute, IActionFilter
    {

        public void OnActionExecuted(ActionExecutedContext context)
        {

            Console.WriteLine(DateTime.Now + "end...");
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {

            Console.WriteLine(DateTime.Now + "start...");

            //context.Result = new RedirectResult("http://www.cnblogs.com/");

        }
    }
View Code

再运行起来,访问刚才的路由,可以在命令窗体看到如图:

技术分享

刚才说的CheckLoginAttribute中的OnActionExecuting对应的是调用Controller的Action方法之前执行的部分,上面代码刚才注释的部分context.Result = new RedirectResult("http://www.cnblogs.com/");是跳转到该http://www.cnblogs.com/地址中去,如果放开注释,看到的效果是访问之前Controller路由后直接跳转到了该地址,并且名利窗体中只有start...日志的部分,没有了之前end...的部分,可以看出这里context.Result效果是直接终止了程序继续往下执行;到这里就可以走一个简单登录的验证了,我这里只说下流程,不做具体代码,因为这不是重点哈哈:

1.在OnActionExecuting方法中使用context.HttpContext.Session获取用户登陆的session(当然其他session保存方式除外)

2.使用context.HttpContext.Request.Path获取当前访问的路由地址

3.如果session为空,使用context.Result = new RedirectResult("/Login?returnUrl=" + context.HttpContext.Request.Path);跳转到路由Login中去,returnUrl参数使用来传递登陆有再跳转到当前访问地址中去

 

.ActionFilter中怎么使用依赖注入

这个是值得关注的地方,这个在我的项目最初写的时候遇到的问题;下面是一些分析,可供大家参考:

1.netcore常用的注入方式是通过构造函数注入的

2.通过构造函数注入后,在需要使用Filter的Action中无法通过对应参数个数的构造函数调用;只能调用无参的Filter构造函数

3.注意在需要使用依赖注入的Filter中不用定义无参构造函数(这里实验过了,如果定义TypeFilter将会以无参构造函数为优先创建实例,这样将会是依赖注入失败)

以上就是最开始无法直接使用结构器依赖注入的原因,后面无意中发现一个很有用的过滤器:TypeFilterAttribute,该过滤器可以通过构造函数传递进去的对象实例化,下面我们一起来看下:

首先,我们定义个MyActionFilterAttribute类并且继承TypeFilterAttribute,默认继承构造函数,然后在Controller的Index上方使用这个自定义属性并且传递我们定义的CheckLoginAttribute类型为参数,如下代码:

技术分享
[MyActionFilter(typeof(CheckLoginAttribute))]
        public JsonResult Index()
        {
            var response = new MoResponse();
            response.Status = 1;

            return Json(response);
        }
View Code

MyActionFilterAttribute代码如下:

技术分享
public class MyActionFilterAttribute : TypeFilterAttribute
    {
        public MyActionFilterAttribute(Type type) : base(type)
        {

        }
    }
View Code

好了,咋们再自定义个简单的服务,并且把定义的服务在Startup.cs文件中增加代码services.AddTransient<LogService>();注入服务,服务要求是定义个方法,在命令框中输出hello...,如下代码:

技术分享
 public class LogService
    {
      
        public async void _LogRequest()
        {

            await Task.Run(() =>
             {

                 for (int i = 0; i < 10; i++)
                 {
                     Console.WriteLine(DateTime.Now + "hello...");
                 }
             });
        }
    }
View Code

再来,CheckLoginAttribute中增加代码如:

技术分享
 1 public class CheckLoginAttribute : Attribute, IActionFilter
 2     {
 3         private readonly LogService _logService;
 4         public CheckLoginAttribute(LogService logService)
 5         {
 6 
 7             _logService = logService;
 8         }
 9 
10         public void OnActionExecuted(ActionExecutedContext context)
11         {
12 
13             Console.WriteLine(DateTime.Now + "end...");
14         }
15 
16         public void OnActionExecuting(ActionExecutingContext context)
17         {
18 
19             Console.WriteLine(DateTime.Now + "start...");
20 
21             var path = context.HttpContext.Request.Path;
22             //context.Result = new RedirectResult($"/Login?returnUrl={path}");
23 
24             _logService._LogRequest();
25         }
26     }
View Code

好了咋们一起dotnet run看到的效果如:

技术分享

这个依赖注入到Filter中就成功了,其实上面定义的MyActionFilterAttribute也可以看做多余吧,因为在Action上使用的其实就是TypeFilterAttribute自身的构造函数方法,咋们可以直接在Action上使用TypeFilter如图:

技术分享

两者效果是一样的,只是定义一个Filter可以记录其他的日志信息或干其他的事情罢了;这次分享的内容就是这样了,不知道描述的是否清晰,希望多多支持,谢谢。

文章来自:http://www.cnblogs.com/wangrudong003/p/5750583.html
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3