Free Web Development Online Tutorials, Learn to Code
Asp.net Core Middleware Example, Free Asp.net Core Tutorial for Beginners, Asp.net Core MVC, Razor Page

Asp.net Core Middleware Example

Asp.net Core middleware Tutorial for beginners step by step, you will learn how to implement middleware in http pipeline.

Middleware in Asp.Net Core

"Middleware is software that's assembled into an app pipeline to handle requests and responses." -MSDN

Middleware is an external components that form a pipeline between a server and application, to inspect, route, modify request and response messages for that specific purpose.

Middleware has a number of application components that are incorporated into the ASP.NET HTTP pipeline.

We can develop a code with a purpose to manipulate the requests and responses.

Or you can say Middleware is a mechanism that allows to manipulate Http-Pipeline, which was not possible in earlier Asp.net Framework (HttpHandlers, HttpModules)

Add Middleware in Pipeline

You can sdd Middleware in Pipeline using IApplicationBuilder, here we see how we can add a new service to Middleware, Some commonly used middleware are UseMvc(), UseAuthentication(), UseDeveloperExceptionPage()

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
 {     
    app.UseMvc();
    app.UseAuthentication();
    app.UseDeveloperExceptionPage();
    app.UseStaticFiles(); 
}		

Middlewares are executed in the order they are configured. You can change the order; the first configured middleware will get executed first.

Custom Middleware

You can build your custom middleware and add to request pipeline in Startup Configure method

Here are the namespace you required to create a custom Middleware, here in example we have created "WTRCustomMiddleware"

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System.Threading.Tasks;
Create the Middleware class
    public class WTRCustomMiddleware
    {
        private readonly RequestDelegate _next;
    public WTRCustomMiddleware(RequestDelegate next)
    {
        _next = next;
    }
    public async Task InvokeAsync(HttpContext context)
    {
        await context.Response.WriteAsync("- Before Message -  \n\r");
        await _next(context);
        await context.Response.WriteAsync("\n\r - After Message - ");
    }
    }

Now add a static extensions class for extension method

    public static class WTRCustomMiddlewareExtensions
    {
        public static IApplicationBuilder UseWTRCustomMiddleware(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<WTRCustomMiddleware>();
        }
    }

Now your custom middleware class is ready simply add to pipeline, open your startup.cs class, place the following code in Configure method.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{ app.UseWTRCustomMiddleware();
app.Run(async (context) =>
    {
        await context.Response.WriteAsync("Welcome to WebTrainingRoom!");
    });
} 

You should also look at the examples below

Comment
Name
Email
Website
Subscribe