前言:
眼前姐妹们对“mvc action过滤器”大体比较关注,我们都想要分析一些“mvc action过滤器”的相关资讯。那么小编也在网摘上网罗了一些对于“mvc action过滤器””的相关内容,希望姐妹们能喜欢,看官们快快来学习一下吧!(注:本文参照 NickChapsas的Attributes get a feature long-overdue in C# 11)
今天看一个泛型特性的例子,这个功能在C#11才受支持。
在asp.net core mvc中,可以给action添加filter,达到拦截作用,实现如下:
public class MyFilter : IAsyncActionFilter{ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { Console.WriteLine("Action前执行"); await next(); Console.WriteLine("Action后执行"); }}
使用方式,在Action上添加ServiceFilter特性即可,如下:
[ServiceFilter(typeof(MyFilter))] public IEnumerable<WeatherForecast> Get(){ return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = Random.Shared.Next(-20, 55), Summary = Summaries[Random.Shared.Next(Summaries.Length)] }) .ToArray();}
在运行前记得把MyFilter注放到Service容器中:
builder.Services.AddScoped<MyFilter>();
为了支持C#11,在项目文件.csproj中,PropertyGroup中添加一行<LangVersion>preview</LangVersion>
<Project Sdk="Microsoft.NET.Sdk.Web"> <PropertyGroup> <TargetFramework>net7.0</TargetFramework> <Nullable>enable</Nullable> <ImplicitUsings>enable</ImplicitUsings> <LangVersion>preview</LangVersion> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.0-preview.4.22251.1" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" /> </ItemGroup></Project>
现在就可以定义一个继承IFilterFactory的特性类了,并且是泛型的。
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]public class GSWFilterAttribute<TFilter> : Attribute, IFilterFactory, IOrderedFilter where TFilter : IAsyncActionFilter{ public bool IsReusable { get; set; } public int Order { get; set; } public IFilterMetadata CreateInstance(IServiceProvider serviceProvider) { if (serviceProvider != null) { var filter = (IFilterMetadata)serviceProvider.GetRequiredService(typeof(TFilter)); if (filter is IFilterFactory filterFactory) { filter = filterFactory.CreateInstance(serviceProvider); } return filter; } else { throw new ArgumentNullException(nameof(serviceProvider)); } }}
使用时,直接把泛型类型放上就可以了,如下:
[GSWFilter<MyFilter>]public IEnumerable<WeatherForecast> Get(){ return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateTime.Now.AddDays(index), TemperatureC = Random.Shared.Next(-20, 55), Summary = Summaries[Random.Shared.Next(Summaries.Length)] }) .ToArray();}
虽然两种方法实现的功能是一样的,但后一种看起来更优雅一些。同时说明一下,如果多个MyFilter功能的过滤器,可以增加Order属性,如下:
[GSWFilter<MyFilter1>(Order = 2)][GSWFilter<MyFilter2>(Order = 1)]public IEnumerable<WeatherForecast> Get()
标签: #mvc action过滤器