OWIN app.use vs app.run vs app.map OWIN app.use vs app.run vs app.map asp.net asp.net

OWIN app.use vs app.run vs app.map


app.use inserts a middleware into the pipeline which requires you to call the next middleware by calling next.Invoke().

app.run inserts a middleware without a next, so it just runs.

With app.map you can map paths, which get evaluated at runtime, per request, to run certain middleware only if the request path matches the pattern you mapped.

See docs for use and run and map for more details


When dealing with a request we use IApplicationBuilder. And we have four methods available to interact with a request:

  • Use
  • Run
  • Map
  • MapWhen

These are called Request Delegates.

Use:

Adds a middleware to the application pipeline and it can either passthe request to next delegate or it can end the request (short- circuitrequest pipeline). It is the most commonly used method to interactwith middleware.

Map

We use Map to connect a request path with another middleware. Thatmiddleware can use any of the other mentioned request delegates.

MapWhen

Behaves almost the same as Map except that we can specify the detailedcondition by using a HttpContext object. We could check for URL,headers, query strings, cookies, etc).

Run

Generate a response and short-circuit therequest

Please also read this article for more information.