简体   繁体   English

为什么我必须在ASP 5中的app.UseMvc之前调用app.UseErrorHandler才能使用它?

[英]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.

相关问题 用 app.UserRouting() 替换 app.UseMvc() 给我们错误“请求匹配多个端点” - Replacing app.UseMvc() with app.UserRouting() gives us error `The request matched multiple endpoints` 为什么我无法从我的应用程序中的其他类访问App.xaml.cs? - Why do I not have access to App.xaml.cs from other classes in my app? 如何从另一个ASP.NET应用程序调用codebehind方法? - How do I call codebehind method from another ASP.NET app? 如何在ASP.NET MVC Web App中使用Ninject? - How do I work with Ninject in an ASP.NET MVC Web App? 为什么 Azure 在向应用程序发送请求之前解码请求的 URL - Why do Azure decode Requested URL before sending request to app 为什么控制台应用程序和 WinForms 之间的性能差异如此之大? - Why do I have such a large performance difference between a Console app and WinForms? 我必须在app.config中有动态WCF条目吗 - Do I have to have an entry in app.config for dynamic WCF 为什么我有时会有app.config,有时还有一个binaryname.dll.config文件? - Why do I sometimes have an app.config and other times a binaryname.dll.config file? Ember.js与现有的.NET Server App:我需要更改哪些内容才能与Ember-Data配合使用? - Ember.js with existing .NET Server App: what do I have to change to work well with Ember-Data? 为什么必须在DataGridViewRow上调用Cast &lt;&gt;扩展方法? - Why do I have to call the Cast<> extension method on a DataGridViewRow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM