<i id='2Y0qL'><tr id='2Y0qL'><dt id='2Y0qL'><q id='2Y0qL'><span id='2Y0qL'><b id='2Y0qL'><form id='2Y0qL'><ins id='2Y0qL'></ins><ul id='2Y0qL'></ul><sub id='2Y0qL'></sub></form><legend id='2Y0qL'></legend><bdo id='2Y0qL'><pre id='2Y0qL'><center id='2Y0qL'></center></pre></bdo></b><th id='2Y0qL'></th></span></q></dt></tr></i><div id='2Y0qL'><tfoot id='2Y0qL'></tfoot><dl id='2Y0qL'><fieldset id='2Y0qL'></fieldset></dl></div>
    1. <small id='2Y0qL'></small><noframes id='2Y0qL'>

          <bdo id='2Y0qL'></bdo><ul id='2Y0qL'></ul>

        <tfoot id='2Y0qL'></tfoot>

        <legend id='2Y0qL'><style id='2Y0qL'><dir id='2Y0qL'><q id='2Y0qL'></q></dir></style></legend>

        ASP.NET Core MVC(前 MVC 6)Razor 错误未被异常过滤器捕获

        时间:2023-07-10
        <tfoot id='lfCAK'></tfoot>

        1. <i id='lfCAK'><tr id='lfCAK'><dt id='lfCAK'><q id='lfCAK'><span id='lfCAK'><b id='lfCAK'><form id='lfCAK'><ins id='lfCAK'></ins><ul id='lfCAK'></ul><sub id='lfCAK'></sub></form><legend id='lfCAK'></legend><bdo id='lfCAK'><pre id='lfCAK'><center id='lfCAK'></center></pre></bdo></b><th id='lfCAK'></th></span></q></dt></tr></i><div id='lfCAK'><tfoot id='lfCAK'></tfoot><dl id='lfCAK'><fieldset id='lfCAK'></fieldset></dl></div>
        2. <small id='lfCAK'></small><noframes id='lfCAK'>

          <legend id='lfCAK'><style id='lfCAK'><dir id='lfCAK'><q id='lfCAK'></q></dir></style></legend>

                <bdo id='lfCAK'></bdo><ul id='lfCAK'></ul>

                    <tbody id='lfCAK'></tbody>

                • 本文介绍了ASP.NET Core MVC(前 MVC 6)Razor 错误未被异常过滤器捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  背景:使用 Dapper 和存储库模式的 ASP.NET 5 (ASP.NET Core 1.0) MVC 6 应用程序

                  Background: ASP.NET 5 (ASP.NET Core 1.0) MVC 6 application using Dapper and the Repository Pattern

                  显然,与其他所有网站/应用程序一样,我正在尝试消除我网站中弹出的大多数/所有异常.

                  Obviously, like with every other website/app, I'm trying to eliminate most/all of the exceptions that popup in my website.

                  我实现了一个 ExceptionFilter 来捕获所有未处理的异常,如下所示:

                  I implemented an ExceptionFilter in order to catch all unhandled exceptions like this:

                  public class UnhandledExceptionFilter : ActionFilterAttribute, IExceptionFilter
                  {
                      private readonly IErrorRepo _errorRepo;
                  
                      public UnhandledExceptionFilter(IErrorRepo errorRepo)
                      {
                          _errorRepo = errorRepo;
                      }
                  
                      public void OnException(ExceptionContext context)
                      {
                          try
                          {
                              _errorRepo.SaveException(context.Exception);
                          }
                          catch { }
                      }
                  }
                  

                  当错误来自 C# 代码时,这很有效.但是我故意在我的 razor 视图(cshtml 文件)中添加了错误,而这些错误并没有被这个过滤器捕获.

                  This works great when the error comes from C# code. But I've purposely put in errors in my razor views (cshtml files) and those are NOT getting caught by this filter.

                  是否需要继承其他属性/接口才能捕获 razor 异常?

                  Is there an additional attribute/interface that I need to inherit in order to catch razor exceptions?

                  更新:

                  这里是指定过滤器属性的地方,在configureServices方法的startup.cs文件中.

                  Here's where the filter attribute is specified, in the startup.cs file in the ConfigureServices method.

                      services.AddMvc(options =>
                      {
                          options.Filters.Add(new UnhandledExceptionFilter(new ErrorRepo(Configuration)));
                      });
                  

                  推荐答案

                  这样做的诀窍不在于属性 - 它是通过添加中间件提供程序.一旦您的中间件在管道中,您将能够捕获任何时候抛出的异常(因此不再需要您的属性).

                  The trick to doing this is not in the attribute - it's by adding a middleware provider. Once your middleware is in the pipeline, you'll be able to catch exceptions thrown at any point (so your attribute will no longer be needed).

                  这实际上是要记录错误的东西.我已经复制了我从您的 IErrorRepo 界面中看到的内容,但是您当然可以对其进行修改以包含传递到下面的 Log 方法中的任何附加信息.

                  This is the thing that's actually going to log errors. I've copied what I've seen from your IErrorRepo interface, but of course you could modify it to include any of the additional information passed into the Log method below.

                  public class UnhandledExceptionLogger : ILogger
                  {
                      private readonly IErrorRepo _repo;
                  
                      public UnhandledExceptionLogger(IErrorRepo repo)
                      {
                          _repo = repo;
                      }
                      public IDisposable BeginScopeImpl(object state) =>
                          new NoOpDisposable();
                  
                      public bool IsEnabled(LogLevel logLevel) =>
                          logLevel == LogLevel.Critical || logLevel == LogLevel.Error;
                  
                      public void Log(LogLevel logLevel, int eventId, object state, Exception exception, Func<object, Exception, string> formatter)
                      {
                          if (IsEnabled(logLevel))
                          {
                              _repo.SaveException(exception);
                          }
                      }
                  
                      private sealed class NoOpDisposable : IDisposable
                      {
                          public void Dispose()
                          {
                          }
                      }
                  }
                  

                  提供者

                  这是创建记录器的工厂.它只会被实例化一次,并在通过管道时创建所有记录器.

                  The Provider

                  This is the factory that will create the logger. It's only going to be instantiated once, and will create all the loggers when it goes through the pipeline.

                  public class UnhandledExceptionLoggerProvider : ILoggerProvider
                  {
                      private readonly IErrorRepo _repo;
                  
                      public UnhandledExceptionLoggerProvider(IErrorRepo repo)
                      {
                          _repo = repo;
                      }
                  
                      public ILogger CreateLogger(string categoryName) =>
                          new UnhandledExceptionLogger(_repo);
                  
                      public void Dispose()
                      {
                      }
                  }
                  

                  注册提供者

                  一旦添加到 ILoggerFactory,它就会在管道中的每个请求上被调用.通常这是通过自定义扩展方法完成的,但我们已经有了很多新代码,所以我将省略这部分.

                  Registering the Provider

                  Once added to the ILoggerFactory, it will be invoked on each request in the pipeline. Often this is done through a custom extension method, but we've already got a lot of new code so I'll omit that part.

                  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
                  {
                      loggerFactory.AddProvider(new UnhandledExceptionLoggerProvider(new ErrorRepo()));
                      // the rest
                  

                  这篇关于ASP.NET Core MVC(前 MVC 6)Razor 错误未被异常过滤器捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将外部项目中的控制器和视图包含到 MVC6 中? 下一篇:如何在 ASP.NET Core 中实现自定义模型验证?

                  相关文章

                  <legend id='m2WWb'><style id='m2WWb'><dir id='m2WWb'><q id='m2WWb'></q></dir></style></legend>
                    <i id='m2WWb'><tr id='m2WWb'><dt id='m2WWb'><q id='m2WWb'><span id='m2WWb'><b id='m2WWb'><form id='m2WWb'><ins id='m2WWb'></ins><ul id='m2WWb'></ul><sub id='m2WWb'></sub></form><legend id='m2WWb'></legend><bdo id='m2WWb'><pre id='m2WWb'><center id='m2WWb'></center></pre></bdo></b><th id='m2WWb'></th></span></q></dt></tr></i><div id='m2WWb'><tfoot id='m2WWb'></tfoot><dl id='m2WWb'><fieldset id='m2WWb'></fieldset></dl></div>
                      <bdo id='m2WWb'></bdo><ul id='m2WWb'></ul>
                    1. <tfoot id='m2WWb'></tfoot>

                      <small id='m2WWb'></small><noframes id='m2WWb'>