concept Run extension in category asp.net

appears as: Run extension, The Run extension
ASP.NET Core in Action

This is an excerpt from Manning's book ASP.NET Core in Action.

Listing 19.1. Creating simple middleware using the Run extension
public void Configure(IApplicationBuilder app) { app.Run(async (context) => #1 { context.Response.ContentType = "text/plain"; #2 await context.Response.WriteAsync( #3 DateTime.UtcNow.ToString()); #3 }); app.UseMvc(); #4 } #1 Uses the Run extension to create a simple middleware that always returns a response #2 You should set the content-type of the response you’re generating. #3 Returns the time as a string in the response. The 200 OK status code is used if not explicitly set. #4 Any middleware added after the Run extension will never execute. !@%STYLE%@! {"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":2,\"ch\":61},{\"line\":2,\"ch\":63}],[{\"line\":12,\"ch\":0},{\"line\":12,\"ch\":2}],[{\"line\":4,\"ch\":61},{\"line\":4,\"ch\":63}],[{\"line\":13,\"ch\":0},{\"line\":13,\"ch\":2}],[{\"line\":5,\"ch\":61},{\"line\":5,\"ch\":63}],[{\"line\":6,\"ch\":61},{\"line\":6,\"ch\":63}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":5,\"ch\":61},{\"line\":5,\"ch\":63}],[{\"line\":6,\"ch\":61},{\"line\":6,\"ch\":63}],[{\"line\":14,\"ch\":0},{\"line\":14,\"ch\":2}],[{\"line\":9,\"ch\":61},{\"line\":9,\"ch\":63}],[{\"line\":15,\"ch\":0},{\"line\":15,\"ch\":2}]]"} !@%STYLE%@!

The Run extension is useful for building simple middleware. You can use it to create basic endpoints that always generate a response. But as the component always generates some sort of response, you must always place it at the end of the pipeline, as no middleware placed after it will execute.