Search code examples
asp.net-core.net-coreasp.net-core-2.1request-pipeline

Use of next() method in Use() method in .net Core?


What happens if I don't call next() method or next.Invoke() in a Use() method in the request pipeline, will it call the next middleware component or not?


Solution

  • From the docs, emphasis mine:

    Chain multiple request delegates together with Use. The next parameter represents the next delegate in the pipeline. You can short-circuit the pipeline by not calling the next parameter.

    And:

    When a delegate doesn't pass a request to the next delegate, it's called short-circuiting the request pipeline. Short-circuiting is often desirable because it avoids unnecessary work. For example, Static File Middleware can act as a terminal middleware by processing a request for a static file and short-circuiting the rest of the pipeline.

    So if you don't invoke the next item, the pipeline will stop there.