.net core3.1 Filter四种注入方式和异常过滤器
2021-03-17 01:24
阅读:828
YPE html>
标签:and viewport href ilo body http provider name net
.net core 异常过滤器的使用
第一种使用方式(全局注册)
第二种方式(TypeFilter)
第三种方式(ServiceFilter)
第四种方式(IFilterFactory)
CustomExceptionFilterAttribute.cs
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute { private readonly ILogger_logger; private readonly IModelMetadataProvider _modelMetadataProvider; public CustomExceptionFilterAttribute(ILogger logger, IModelMetadataProvider modelMetadataProvider) { _logger = logger; _modelMetadataProvider = modelMetadataProvider; } /// /// 发生异常时 进入 /// /// 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
错误 错误。
处理你的请求时出错。
异常信息:@ViewData["Exception"]
@{ Console.WriteLine(ViewData["Exception"]); }
Ajax请求:
@{ ViewData["Title"] = "Home Page"; }@section scripts{ }Welcome
Learn about building Web apps with ASP.NET Core.
第一种使用方式(全局注册):
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(); //注册 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(); //注册 services.AddControllersWithViews(); }
.net core3.1 Filter四种注入方式和异常过滤器
标签:and viewport href ilo body http provider name net
原文地址:https://www.cnblogs.com/xbzhu/p/13056363.html
下一篇:HTTP 协议
文章来自:搜素材网的编程语言模块,转载请注明文章出处。
文章标题:.net core3.1 Filter四种注入方式和异常过滤器
文章链接:http://soscw.com/index.php/essay/65083.html
文章标题:.net core3.1 Filter四种注入方式和异常过滤器
文章链接:http://soscw.com/index.php/essay/65083.html
评论
亲,登录后才可以留言!