[英]Why do I have to call app.UseErrorHandler before app.UseMvc in ASP 5 for it to work?
I created an ASP.NET 5 application for Web API and installed Asp.Net.Diagnostics from NuGet so I could add my own error handling. 我为Web API创建了一个ASP.NET 5应用程序,并从NuGet安装了Asp.Net.Diagnostics,因此我可以添加自己的错误处理。
What works: (From Configuration function in Startup.cs) 什么有效:(来自Startup.cs中的配置功能)
app.UseErrorHandler("/Home/Error");
app.UseMvc(routes => {
routes.MapRoute("Default", "{controller=Home}/{action=Index}/{id?}");
});
What doesn't work: (app uses normal error handler instead of the custom one) 什么行不通:( app使用普通错误处理程序而不是自定义错误处理程序)
app.UseMvc(/*..configure routes..*/);
app.UseErrorHandler("/Home/Error");
Inside my controller I'm throwing: 在我的控制器里面我扔了:
throw new NotImplementedException("This method is not implemented");
I usually configure my big things first and the details later in case the details get overwritten. 我通常先配置我的大件事,稍后再详细说明,以防细节被覆盖。 In this case, though, I need to configure my big thing (MVC) after my smaller thing (ErrorHandler), or my ErrorHandler doesn't work. 但是,在这种情况下,我需要在我的小东西(ErrorHandler)之后配置我的大事(MVC),否则我的ErrorHandler不起作用。 Why is that? 这是为什么?
Because route order in ASP.NET is important. 因为ASP.NET中的路由顺序很重要。 Configuring big things first and details later is not what the system is expecting. 先配置大件事物,稍后再详述,不是系统所期望的。
ASP.NET checks the first route. ASP.NET检查第一个路由。 If it matches, then it doesn't check the rest of the routes. 如果匹配,则不检查其余路由。
For further details, see this blog post I found. 有关详细信息,请参阅我发现的此博客文章 。 It's for an older version of ASP.NET, but I believe the principles still apply. 它适用于旧版本的ASP.NET,但我相信这些原则仍然适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.