首页 > 其他分享 >251 Resource Filter

251 Resource Filter

时间:2024-06-04 18:59:50浏览次数:21  
标签:TypeFilter FeatureDisabledResourceFilter isDisabled Filter Resource logger 251 p

示例

新建FeatureDisabledResourceFilter.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace CRUDExample.Filters.ResourceFilters
{
    public class FeatureDisabledResourceFilter : IAsyncResourceFilter
    {
        private readonly ILogger<FeatureDisabledResourceFilter> _logger;
        private readonly bool _isDisabled;

        public FeatureDisabledResourceFilter(ILogger<FeatureDisabledResourceFilter> logger, bool isDisabled = true)
        {
            _logger = logger;
            _isDisabled = isDisabled;
        }

        public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
        {
            //TO DO: before logic
            _logger.LogInformation("{FilterName}.{MethodName} - before", nameof(FeatureDisabledResourceFilter), nameof(OnResourceExecutionAsync));
            if (_isDisabled)
            {
                //context.Result = new NotFoundResult(); //404 - Note Found

                context.Result = new StatusCodeResult(501); //501 - Not Implemented
            }
            else 
            {
                await next();
                // TO DO: after logic
            _logger.LogInformation("{FilterName}.{MethodName} - after", nameof(FeatureDisabledResourceFilter), nameof(OnResourceExecutionAsync));
            }
        }
    }
}

PersonsController.cs中添加TypeFilter

//处理Post请求
[Route("[action]")]
[HttpPost]
[TypeFilter(typeof(PersonCreateAndEditPostActionFilter))]
[TypeFilter(typeof(FeatureDisabledResourceFilter))]
public async Task<IActionResult> Create(PersonAddRequest personAddRequest)

执行程序点击Create后显示

PersonsController.cs中添加TypeFilter中添加false参数或者删除筛选器后,程序正常运行

//处理Post请求
[Route("[action]")]
[HttpPost]
[TypeFilter(typeof(PersonCreateAndEditPostActionFilter))]
[TypeFilter(typeof(FeatureDisabledResourceFilter), Arguments = new object[] {false})]
public async Task<IActionResult> Create(PersonAddRequest personAddRequest)

Gitee获取源码:

https://gitee.com/huang_jianhua0101/asp.-net-core-8.git

标签:TypeFilter,FeatureDisabledResourceFilter,isDisabled,Filter,Resource,logger,251,p
From: https://blog.csdn.net/KevinHuang2088/article/details/139451344

相关文章

  • 252 Authorization Filter
    示例新建TokenAuthorizationFilter.csusingMicrosoft.AspNetCore.Mvc;usingMicrosoft.AspNetCore.Mvc.Filters;namespaceCRUDExample.Filters.AuthorizationFilters{  publicclassTokenAuthorizationFilter:IAsyncAuthorizationFilter  {   ......
  • 255 IAlwaysRunResultFilter
    示例新建PersonsAlwaysRunResultFilter.csusingMicrosoft.AspNetCore.Mvc.Filters;namespaceCRUDExample.Filters.ResultFilters{  publicclassPersonsAlwaysRunResultFilter:IAsyncAlwaysRunResultFilter  {    publicasyncTaskOnResult......
  • SpringSecurity6从入门到实战之Filter过滤器回顾
    SpringSecurity6从入门到实战之Filter过滤器回顾如果没有SpringSecurity这个框架,我们应该通过什么去实现客户端向服务端发送请求时,先检查用户是否登录,登录了才能访问.否则重定向到登录页面流程图如下官方文档:https://docs.spring.io/spring-security/reference/6.0......
  • 翻译《The Old New Thing》- What a drag: Dragging a Uniform Resource Locator (URL
    Whatadrag:DraggingaUniformResourceLocator(URL)andtext-TheOldNewThinghttps://devblogs.microsoft.com/oldnewthing/20080313-00/?p=23123RaymondChen 2008年03月13日 麻烦的拖拽:拖拽统一资源定位符(URL)和文本简要        这篇文章主要讲......
  • 翻译《The Old New Thing》- What a drag: Dragging a Uniform Resource Locator (URL
    Whatadrag:DraggingaUniformResourceLocator(URL)-TheOldNewThing(microsoft.com)https://devblogs.microsoft.com/oldnewthing/20080312-00/?p=23133RaymondChen 2008年03月12日麻烦的拖拽:拖拽统一资源定位符(URL)简要本文介绍了如何在Windows程序中实......
  • SpringBootWeb 篇-深入了解 Filter 过滤器与 Interceptor 拦截器(实现登录校验 Filter
    ......
  • 记一次NoResourceFoundException: No static resource异常
    org.springframework.web.servlet.resource.NoResourceFoundException:Nostaticresource是Controller层,Rest接口的定义错误ApiPost工具访问,调用接口报错改为@RestController注解......
  • JavaWeb笔记整理+图解——Filter过滤器
    欢迎大家来到这一篇章——Filter过滤器监听器和过滤器都是JavaWeb服务器三大组件(Servlet、监听器、过滤器)之一,他们对于Web开发起到了不可缺少的作用。ps:想要补充Java知识的同学们可以移步我已经完结的JavaSE笔记,里面整理了大量详细的知识点和图解,可以帮你快速掌握Java编程的......
  • Springboot报class path resource [xxxxx.json] cannot be resolved to URL because i
    当Springboot解析resources文件下的json文件时,在本地环境好用,部署到服务器上找不到文件内容报错classpathresource[xxxxx.json]cannotberesolvedtoURLbecauseitdosenotexist问题排查(1)pom.xml文件配置<build><resources><resource><d......
  • Neural Filters:着色
    Ps菜单:滤镜/NeuralFilters/颜色/着色NeuralFilters/COLOR/Colorize着色Colorize滤镜可以对黑白照片自动着色,也为彩色照片简化色彩提供了有力的支持。“着色”滤镜利用深度学习技术,对输入的黑白或彩色照片进行分析,识别图像中的物体、背景和其他元素。然后,根据图像内容和......