• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

asp.netcore3.1Filter四种注入方式和异常过滤器

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

.net core 异常过滤器的使用

  CustomExceptionFilterAttribute.cs

  public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
    {
        private readonly ILogger<CustomExceptionFilterAttribute> _logger;
        private readonly IModelMetadataProvider _modelMetadataProvider;
        public CustomExceptionFilterAttribute(ILogger<CustomExceptionFilterAttribute> logger, IModelMetadataProvider modelMetadataProvider)
        {
            _logger = logger;
            _modelMetadataProvider = modelMetadataProvider;
        }
        /// <summary>
        /// 发生异常时 进入
        /// </summary>
        /// <param name="context"></param>
        public override void OnException(ExceptionContext context)
        {
            this._logger.LogError($"在响应 {context.HttpContext.Request.Path} 时出现异常,信息:{context.Exception.Message}");

            if (!context.ExceptionHandled)        //异常未被处理
            {
                if (this.IsAjaxRequest(context.HttpContext.Request))//ajax请求
                {
                    context.Result = new JsonResult(new
                    {
                        Result = false,
                        Msg = "发生错误,请联系管理员",
                        DebugMessage = context.Exception.Message
                    });//中断式---请求到这里结束了,不再继续Action
                }
                else
                {
                    var result = new ViewResult { ViewName = "~/Views/Shared/Error.cshtml" };
                    result.ViewData = new ViewDataDictionary(_modelMetadataProvider,
                                                                context.ModelState);
                    result.ViewData.Add("Exception", context.Exception.Message);
                    context.Result = result;
                }
                context.ExceptionHandled = true;//异常已被处理
            }
        }

        private bool IsAjaxRequest(HttpRequest request)
        {
            string header = request.Headers["X-Requested-With"];
            return "XMLHttpRequest".Equals(header);
        }
    }

  Error.cshtml

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width" />
    <title>错误</title>
</head>
<body>
    <hgroup>
        <h1>错误。</h1>
        <h2>处理你的请求时出错。</h2>
        <h3>异常信息:@ViewData["Exception"]</h3>
        @{ 

            Console.WriteLine(ViewData["Exception"]);
        }
    </hgroup>
</body>
</html>

 

Ajax请求:

@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    <button id="b01" type="button">Change Content</button>
</div>
@section scripts{
    <script>
        $("#b01").click(function () {

            $.ajax({
                url: 'http://localhost:5177/Custom',//地址
                dataType: 'json',//数据类型
                type: 'Post',//类型
                timeout: 2000,//超时
                //请求成功
                success: function (data, status) {
                    console.log(data.result);
                    console.log(data.msg);
                    console.log(data.debugMessage);
                }
            });
        });
    </script>
}

 

第一种使用方式(全局注册):

   public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews(options =>
         {
              options.Filters.Add(typeof(CustomExceptionFilterAttribute)); //全局注册异常
          });
          services.AddControllersWithViews();
    }

第二种方式(TypeFilter):

[TypeFilter(typeof(CustomExceptionFilterAttribute))]
    public class CustomController : Controller
    {
        public IActionResult Index()
        {
            Console.WriteLine("Custom/Index");
            int i = 0;
            int k = 10;
            int j = k / i;  //这里会异常
            return View();
        }
    }

第三种方式(ServiceFilter):

[ServiceFilter(typeof(CustomExceptionFilterAttribute))]
    public class CustomController : Controller
    {
        public IActionResult Index()
        {
            Console.WriteLine("Custom/Index");
            int i = 0;
            int k = 10;
            int j = k / i;  //这里会异常
            return View();
        }
    }

     public void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<CustomExceptionFilterAttribute>(); //注册
            services.AddControllersWithViews();
        }

 第四种方式(IFilterFactory)

 

 public class CustomIOCFilterFactoryAttribute : Attribute, IFilterFactory
    {
        Type _FilterType = null;
        public CustomIOCFilterFactoryAttribute(Type FilterType)
        {
            _FilterType = FilterType;
        }
        public bool IsReusable => true;

        public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
        {
            return (IFilterMetadata)serviceProvider.GetService(this._FilterType);
        }
    }


    [CustomIOCFilterFactoryAttribute(typeof(CustomExceptionFilterAttribute))]
    public class CustomController : Controller
    {
        public IActionResult Index()
        {
            Console.WriteLine("Custom/Index");
            int i = 0;
            int k = 10;
            int j = k / i;  //这里会异常
            return View();
        }
    }

  public void ConfigureServices(IServiceCollection services)
      {
          services.AddTransient<CustomExceptionFilterAttribute>(); //注册
          services.AddControllersWithViews();
      }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
asp.netwebapi自定义输出结果类似Response.Write()发布时间:2022-07-10
下一篇:
asp.netweb大文件上传带进度条实例代码发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap