如何在ASP.NET Core中获取中间件列表

问题描述:

如何获取活动中间件的列表? 如何获取特定URL的中间件列表(每个URL可能已向管道添加了一组不同的中间件)?

How do I get a list of active middlewares? How do I get a list of middlewares for specific url (each url may have a different set of middlewares added to the pipeline)?

我想通过使用某些常见的构建器扩展(例如UseMvc()app.UseIdentity();

I would like to know which middlewares are being added by using some of the common builder extensions like UseMvc() or app.UseIdentity();

我知道我可以检查每个扩展的源代码.有运行时方法可以做到这一点吗?

I know I could check the source code of each extension. Is there a runtime method to get this?

不,您不能.当您将中间件添加到管道中时,它已解析为Func<RequestDelegate, RequestDelegate>. 组件保存在

No, you can't. When you add a middleware to the pipeline, it's resolved to a Func<RequestDelegate, RequestDelegate>. The components are saved in a private field in the ApplicationBuilder implementation. You could however bake an extension method with some reflection magic to determine the actual middleware type, but it's not trivial.