对ASP.NET Core异常处理中间件的条件配置

问题描述

我正在调用相同的ASP.NET Core 3.1中间件,但具有不同的选项。例如,我的Startup.Configure()如下:

var exceptionHandlerOptions = new ExceptionHandlerOptions();
app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"),appBuilder =>
{
  // Error in WebApi controller
  exceptionHandlerOptions.ExceptionHandlingPath = "/api-exception-handler";
  appBuilder.UseMiddleware<exceptionhandlermiddleware>(Options.Create(exceptionHandlerOptions));
});
app.UseWhen(context => !context.Request.Path.StartsWithSegments("/api"),appBuilder =>
{
  // Error in Mvc controller
  exceptionHandlerOptions.ExceptionHandlingPath = "/mvc-exception-handler";
  appBuilder.UseMiddleware<exceptionhandlermiddleware>(Options.Create(exceptionHandlerOptions));
});

但是,我无法使其正常运行。 ExceptionHandlingPath始终为"/mvc-exception-handler"

P.S。为了简化起见,我整理了示例。我的实际实现虽然有所不同我在IAppBuilder上有一个扩展方法,用于包装Exceptionhandlermiddleware的UseWhen。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)