How to inject a middleware correctly having the route info? #2118
-
I have an Ocelot web API solution in which requests must be checked by a The thing is when i add my middleware like: app.UseMiddleware<CaptchaPayloadMiddleware>();
app.UseOcelot().Wait(); The response is sent back nicely but i cannot access route info, and when i add my middleware like: app.UseOcelot().Wait();
app.UseMiddleware<CaptchaPayloadMiddleware>(); I have access to route info, but cannot change response as it already started. Is this even possible to do with Ocelot? Thanks |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hello Jorge!
To make route information available in the
Please be aware that Ocelot utilizes a unique internal middleware signature, which differs from the classic ASP.NET middleware. For reference, see the OcelotMiddlewareExtensions and OcelotPipelineExtensions classes. The Ocelot kernel, which is built upon the ASP.NET middleware architecture, processes requests. If you insert your own middleware prior to Ocelot's, no information will be available as the request has not yet been processed by the Ocelot kernel. Therefore, it is recommended to use the Middleware Injection feature exclusively.
Certainly! Adding your own middleware after the I hope this is helpful! |
Beta Was this translation helpful? Give feedback.
Hello Jorge!
Welcome to Ocelot world! 🐯
To make route information available in the
HttpContext
, including Route Metadata, you need to:CaptchaPayloadMiddleware
into an anonymous or namedFunc<HttpContext, Func<Task>, Task>
delegate.Func
delegate to anOcelotPipelineConfiguration
instance within theUseOcelot
method, utilizing the Middleware Injection feature.Please be aware that Ocelot utilizes a unique internal middleware signature, which differs from the classic ASP.NET middlewar…