Use

The Use method adds a delegate to the application request pipelines. Look at the following screenshot for the signature of this method:

Signature of Use method

As we discussed in the previous section, the middleware methods can short circuit the request pipeline or pass the request to the next delegate.

Short-circuiting a request is nothing but ending a request.

Look at the following code for the Use method:

public void Configure(IApplicationBuilder app)
{
async Task Middleware(HttpContext context, Func<Task> next)
{
//other stuff
await next.Invoke();
//other stuff
}
app.Use(Middleware);
}

In the preceding code, I have tried to explain the dummy implementation of the Use method with the help of a local function. Here, you can see that Middleware is invoking or passing the request to the next delegate, before or after await next.Invoke();. You can write/implement other code phrases, but these phrases should not send responses to the client, such as those that write output, produce the 404 status, and so on.

Local functions are the methods that are declared within a method and can be called within the scope of the method itself. These methods can only be used by another method.