aspdotnet_middleware.md - brainchildservices/curriculum GitHub Wiki

SLIDE-1

Middleware

Lets Start with Real Life Example:- Consider a Middleware as a water supply, when the water comes through the given pipe connection and reaches to a filter, in this case, we can consider the filter as the Middleware. Then the Filter will Clean the water and send/store it as per the Requirement. in this case we can consider Filter as our Middleware

In ASP.NET Core, middleware are C# classes that can handle an HTTP request or response. Middleware can either:

  • Handle an incoming HTTP request by generating an HTTP response.
  • Process an incoming HTTP request, modify it, and pass it on to another piece of middleware.
  • Process an outgoing HTTP response, modify it, and pass it on to either another piece of middleware, or the ASP.NET Core web server.

SLIDE-2

In ASP.NET Core there are so many built-in Middleware components that are already made available that you can use directly. If you want then you can also create your own Middleware components in asp.net core applications.

image

As you can see, Whenever a Request comes it will go through the given middleware and perform required actions and give a response as per the user requirements.

SLIDE-3

In the given code, we have used a few Middleware.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace newWebapp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddRazorPages();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }
    }
}

SLIDE-4

In the later example, there are several Middleware are used for different purposes.

  • UseDeveloperExceptionPage - Using for Exceptions in Developer Mode. This middleware help a developer to find where the error with ease.
  • UseExceptionHandler - This will help us to prevent the users to see the Actual Reason for the Error in hosted Web app.
  • UseHttpsRedirection - This Middleware help us Redirect all HTTP requests to HTTPS.
  • UseRouting - Find Endpoint, Adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. Will discuss about this more in future sessions.
  • UseEndpoints - Execute Endpoint, Adds endpoint execution to the middleware pipeline. It runs the delegate associated with the selected endpoint.

in the above example if we add the app.UseEndpoints before app.UseRouting(); it won't work properly.

Note:- The Middleware are should be in an order to Work, other wise you won't be able to get the Required response.

  • There are many more Middleware are available. Refer :- MiddleWare