{"id":245,"date":"2025-07-30T09:01:28","date_gmt":"2025-07-30T09:01:28","guid":{"rendered":"https:\/\/www.dotnetdevelopers.us\/blogs\/?p=245"},"modified":"2025-07-30T09:01:30","modified_gmt":"2025-07-30T09:01:30","slug":"asp-net-core","status":"publish","type":"post","link":"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/","title":{"rendered":"Creating Custom Middleware in ASP.NET Core \u2013 A Simple Step-by-Step Guide"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-light-blue ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#_What_is_Middleware_in_ASPNET_Core\" >&nbsp;What is Middleware in ASP.NET Core?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#_Built-In_vs_Custom_Middleware\" >&nbsp;Built-In vs. Custom Middleware<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#_How_to_Create_Custom_Middleware_in_ASPNET_Core\" >\u00a0How to Create Custom Middleware in ASP.NET Core<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#Step_1_Create_a_New_ASPNET_Core_Project\" >Step 1: Create a New ASP.NET Core Project<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#Step_2_Create_a_Middleware_Class\" >Step 2: Create a Middleware Class<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#Step_3_Create_an_Extension_Method_Optional_but_Neat\" >Step 3: Create an Extension Method (Optional but Neat)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#_Step_4_Register_Your_Middleware_in_the_Pipeline\" >&nbsp;Step 4: Register Your Middleware in the Pipeline<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#_Real-World_Use_Cases_of_Custom_Middleware\" >&nbsp;Real-World Use Cases of Custom Middleware<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#_Pro_Tips\" >&nbsp;Pro Tips<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#Q1_Do_I_need_to_create_a_new_class_every_time_I_want_to_make_middleware\" >Q1: Do I need to create a new class every time I want to make middleware?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#Q2_Can_middleware_access_route_data_or_query_strings\" >Q2: Can middleware access route data or query strings?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#Q3_Can_I_use_multiple_custom_middleware\" >Q3: Can I use multiple custom middleware?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#Q4_Is_it_possible_to_handle_exceptions_in_middleware\" >Q4: Is it possible to handle exceptions in middleware?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/www.dotnetdevelopers.us\/blogs\/asp-net-core\/#_Wrapping_It_All_Up\" >&nbsp;Wrapping It All Up<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n<p>If you&#8217;re diving into ASP.NET Core, you&#8217;ll hear the word middleware tossed around a lot. But don\u2019t worry, it\u2019s not as scary or complex as it sounds.<\/p>\n\n\n\n<p>Middleware is essentially a piece of code that sits in the pipeline, processing incoming HTTP requests and outgoing responses. Think of it like a bouncer at a club, it checks who&#8217;s coming in, can change the rules, or even block entry based on certain conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"_What_is_Middleware_in_ASPNET_Core\"><\/span><strong>&nbsp;What is Middleware in ASP.NET Core?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Middleware is software that handles requests and responses. In ASP.NET Core, each request to your web application passes through a pipeline composed of middleware components.<\/p>\n\n\n\n<p>Each component:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Receives the request<br><\/li>\n\n\n\n<li>Does something with it (like logging, authentication, etc.)<br><\/li>\n\n\n\n<li>Either passes it to the next middleware or short-circuits it and ends the pipeline<strong><br><\/strong><\/li>\n<\/ol>\n\n\n\n<p>Here\u2019s a simple view of how it flows:<\/p>\n\n\n\n<p>scss<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>Incoming Request \u2192 Middleware 1 \u2192 Middleware 2 \u2192 Middleware 3 \u2192 Endpoint (Controller)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u2191&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2191<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Do something) &nbsp; &nbsp; &nbsp; &nbsp; (Maybe modify response)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"_Built-In_vs_Custom_Middleware\"><\/span><strong>&nbsp;Built-In vs. Custom Middleware<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>ASP.NET Core has built-in middleware like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>UseRouting<br><\/li>\n\n\n\n<li>UseAuthentication<br><\/li>\n\n\n\n<li>UseAuthorization<br><\/li>\n\n\n\n<li>UseEndpoints<br><\/li>\n<\/ul>\n\n\n\n<p>But sometimes, you need your logic. Maybe you want to log requests in a specific format, check headers, or add a custom token validator. That\u2019s where custom middleware comes in.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"_How_to_Create_Custom_Middleware_in_ASPNET_Core\"><\/span><strong>\u00a0How to Create Custom Middleware in <a href=\"https:\/\/www.dotnetdevelopers.us\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/www.dotnetdevelopers.us\/\" rel=\"noreferrer noopener\">ASP.NET <\/a>Core<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Alright, let\u2019s roll up our sleeves and build a custom middleware from scratch.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_1_Create_a_New_ASPNET_Core_Project\"><\/span><strong>Step 1: Create a New ASP.NET Core Project<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>If you haven\u2019t already, fire up a new project:<\/p>\n\n\n\n<p>bash<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>dotnet new web -n CustomMiddlewareDemo<\/p>\n\n\n\n<p>cd CustomMiddlewareDemo<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_2_Create_a_Middleware_Class\"><\/span><strong>Step 2: Create a Middleware Class<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Let\u2019s say we want to log the time taken for each request. Create a new class in your project called RequestTimingMiddleware.cs.<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>public class RequestTimingMiddleware<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;private readonly RequestDelegate _next;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;public RequestTimingMiddleware(RequestDelegate next)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_next = next;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;public async Task InvokeAsync(HttpContext context)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var watch = Stopwatch.StartNew();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;await _next(context); \/\/ Call the next middleware in the pipeline<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;watch.Stop();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var timeTaken = watch.ElapsedMilliseconds;<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine($&#8221;Request took {timeTaken} ms&#8221;);<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>What\u2019s happening here?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>RequestDelegate _next is the next middleware in line.<br><\/li>\n\n\n\n<li>InvokeAsync() is the method ASP.NET Core uses to execute your middleware.<br><\/li>\n\n\n\n<li>We start a stopwatch before calling the next middleware, then log the time after it finishes.<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Step_3_Create_an_Extension_Method_Optional_but_Neat\"><\/span><strong>Step 3: Create an Extension Method (Optional but Neat)<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>To make your middleware easy to add, create an extension method in a class called RequestTimingMiddlewareExtensions.cs:<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>public static class RequestTimingMiddlewareExtensions<\/p>\n\n\n\n<p>{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;public static IApplicationBuilder UseRequestTiming(this IApplicationBuilder builder)<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;{<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return builder.UseMiddleware&lt;RequestTimingMiddleware&gt;();<\/p>\n\n\n\n<p>&nbsp;&nbsp;&nbsp;&nbsp;}<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>This lets us add the middleware in a clean, readable way in Startup.cs or Program.cs.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"_Step_4_Register_Your_Middleware_in_the_Pipeline\"><\/span><strong>&nbsp;Step 4: Register Your Middleware in the Pipeline<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In .NET 6 or later, you\u2019ll be using Program.cs. Add your middleware there:<\/p>\n\n\n\n<p>csharp<\/p>\n\n\n\n<p>CopyEdit<\/p>\n\n\n\n<p>var builder = WebApplication.CreateBuilder(args);<\/p>\n\n\n\n<p>var app = builder.Build();<\/p>\n\n\n\n<p>app.UseRequestTiming(); \/\/ Our custom middleware<\/p>\n\n\n\n<p>app.MapGet(&#8220;\/&#8221;, () =&gt; &#8220;Hello World!&#8221;);<\/p>\n\n\n\n<p>app.Run();<\/p>\n\n\n\n<p>Now, every time you hit the endpoint, you\u2019ll see in the console how long the request took.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"_Real-World_Use_Cases_of_Custom_Middleware\"><\/span><strong>&nbsp;Real-World Use Cases of Custom Middleware<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Here are a few things you can use custom middleware for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Logging request and response data<br><\/li>\n\n\n\n<li>Checking API keys in headers<br><\/li>\n\n\n\n<li>Redirecting based on custom logic<br><\/li>\n\n\n\n<li>Handling exceptions in a centralised place<br><\/li>\n\n\n\n<li>Injecting custom response headers<br><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"_Pro_Tips\"><\/span><strong>&nbsp;Pro Tips<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Always keep your middleware lightweight and fast.<br><\/li>\n\n\n\n<li>Middleware is executed in the order you register it, so order matters!<br><\/li>\n\n\n\n<li>Want to short-circuit the pipeline? Just don\u2019t call _next(context).<br><\/li>\n\n\n\n<li>Need services (like logging)? Inject them via the constructor using dependency injection.<br><\/li>\n<\/ul>\n\n\n\n<p class=\"has-text-align-center has-large-font-size\"><strong>&nbsp;FAQs<\/strong><\/p>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1753865597070\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Q1_Do_I_need_to_create_a_new_class_every_time_I_want_to_make_middleware\"><\/span><strong>Q1: Do I need to create a new class every time I want to make middleware?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p> Not necessarily. For simple logic, you can use inline middleware with app.Use().<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1753865615168\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Q2_Can_middleware_access_route_data_or_query_strings\"><\/span><strong>Q2: Can middleware access route data or query strings?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Absolutely. Just use context.Request.Path, context.Request.Query, etc.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1753865636745\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Q3_Can_I_use_multiple_custom_middleware\"><\/span><strong>Q3: Can I use multiple custom middleware?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yes, you can chain as many as needed. Just remember the order!<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1753865656801\" class=\"rank-math-list-item\">\n<h3 class=\"rank-math-question \"><span class=\"ez-toc-section\" id=\"Q4_Is_it_possible_to_handle_exceptions_in_middleware\"><\/span><strong>Q4: Is it possible to handle exceptions in middleware?<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n<div class=\"rank-math-answer \">\n\n<p>Yep! Writing a global error handler middleware is one of the most common use cases.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"_Wrapping_It_All_Up\"><\/span><strong>&nbsp;Wrapping It All Up<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Custom middleware in ASP.NET Core gives you full control over your application\u2019s request and response process. It&#8217;s like inserting your logic right into the veins of your app. Whether it&#8217;s for logging, security, or custom headers, middleware is your go-to.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re diving into ASP.NET Core, you&#8217;ll hear the word middleware tossed around a lot. But don\u2019t worry, it\u2019s not as scary or complex as it sounds. Middleware is essentially a piece of code that sits in the pipeline, processing incoming HTTP requests and outgoing responses. Think of it like a bouncer at a club, it checks who&#8217;s coming in, can change the rules, or even block entry based on certain conditions. &nbsp;What is Middleware in ASP.NET Core? Middleware is software that handles requests and responses. In ASP.NET Core, each request to your web application passes through a pipeline composed of middleware components. Each component: Here\u2019s a simple view of how it flows: scss CopyEdit Incoming Request \u2192 Middleware 1 \u2192 Middleware 2 \u2192 Middleware 3 \u2192 Endpoint (Controller) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\u2191&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \u2191 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(Do something) &nbsp; &nbsp; &nbsp; &nbsp; (Maybe modify response) &nbsp;Built-In vs. Custom Middleware ASP.NET Core has built-in middleware like: But sometimes, you need your logic. Maybe you want to log requests in a specific format, check headers, or add a custom token validator. That\u2019s where custom middleware comes in. \u00a0How to Create Custom Middleware in ASP.NET Core Alright, let\u2019s roll up our sleeves and build a custom middleware from scratch. Step 1: Create a New ASP.NET Core Project If you haven\u2019t already, fire up a new project: bash CopyEdit dotnet new web -n CustomMiddlewareDemo cd CustomMiddlewareDemo Step 2: Create a Middleware Class Let\u2019s say we want to log the time taken for each request. Create a new class in your project called RequestTimingMiddleware.cs. csharp CopyEdit public class RequestTimingMiddleware { &nbsp;&nbsp;&nbsp;&nbsp;private readonly RequestDelegate _next; &nbsp;&nbsp;&nbsp;&nbsp;public RequestTimingMiddleware(RequestDelegate next) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_next = next; &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;public async Task InvokeAsync(HttpContext context) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var watch = Stopwatch.StartNew(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;await _next(context); \/\/ Call the next middleware in the pipeline &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;watch.Stop(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var timeTaken = watch.ElapsedMilliseconds; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine($&#8221;Request took {timeTaken} ms&#8221;); &nbsp;&nbsp;&nbsp;&nbsp;} } What\u2019s happening here? Step 3: Create an Extension Method (Optional but Neat) To make your middleware easy to add, create an extension method in a class called RequestTimingMiddlewareExtensions.cs: csharp CopyEdit public static class RequestTimingMiddlewareExtensions { &nbsp;&nbsp;&nbsp;&nbsp;public static IApplicationBuilder UseRequestTiming(this IApplicationBuilder builder) &nbsp;&nbsp;&nbsp;&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return builder.UseMiddleware&lt;RequestTimingMiddleware&gt;(); &nbsp;&nbsp;&nbsp;&nbsp;} } This lets us add the middleware in a clean, readable way in Startup.cs or Program.cs. &nbsp;Step 4: Register Your Middleware in the Pipeline In .NET 6 or later, you\u2019ll be using Program.cs. Add your middleware there: csharp CopyEdit var builder = WebApplication.CreateBuilder(args); var app = builder.Build(); app.UseRequestTiming(); \/\/ Our custom middleware app.MapGet(&#8220;\/&#8221;, () =&gt; &#8220;Hello World!&#8221;); app.Run(); Now, every time you hit the endpoint, you\u2019ll see in the console how long the request took. &nbsp;Real-World Use Cases of Custom Middleware Here are a few things you can use custom middleware for: &nbsp;Pro Tips &nbsp;FAQs &nbsp;Wrapping It All Up Custom middleware in ASP.NET Core gives you full control over your application\u2019s request and response process. It&#8217;s like inserting your logic right into the veins of your app. Whether it&#8217;s for logging, security, or custom headers, middleware is your go-to.<\/p>\n","protected":false},"author":1,"featured_media":246,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"_links":{"self":[{"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/posts\/245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/comments?post=245"}],"version-history":[{"count":2,"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/posts\/245\/revisions"}],"predecessor-version":[{"id":248,"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/posts\/245\/revisions\/248"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/media\/246"}],"wp:attachment":[{"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/media?parent=245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/categories?post=245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dotnetdevelopers.us\/blogs\/wp-json\/wp\/v2\/tags?post=245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}