Hi, @Alexandre
You could only put condition into the overrided delegating handler to decide next operation. In your preceding code, the ValidateHeaderHandler
is registered with DI. Once registered, AddHttpMessageHandler
will be called, passing in the type for the handler.
---
I think you are looking for a RouterMiddleware
for the specific route middleware.
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
....
app.UseRouter(BuildRouter(app));
app.UseRouting();
...
}
public IRouter BuildRouter(IApplicationBuilder applicationBuilder)
{
var builder = new RouteBuilder(applicationBuilder);
// use middlewares to configure a route
builder.MapMiddlewareGet("/api/v1/user", appBuilder => {
// adds RequestValidationMiddleware delegate to the application's request pipeline and pass the request to next middleware.
appBuilder.Use(RequestValidationMiddleware);
});
builder.MapMiddlewareGet("/api/v2/user", appBuilder => {
// adds a middleware to the application's request pipeline and end the request.
appBuilder.Run(async context => {
await context.Response.WriteAsync("<div>/api/v2/user - Run</div>");
});
});
// builder.MapMiddlewarePost("/api/v2/user"...
return builder.Build();
}
private Func<RequestDelegate, RequestDelegate> RequestValidationMiddleware = next => {
return async context =>
{
await context.Response.WriteAsync("<div>/api/v1/user - RequestValidationMiddleware - Use</div>");
await next(context);
};
};
Test result
---
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Michael Wang