CINXE.COM

Utilizing the IMiddleware interface in Umbraco - Rachel Breeze, Backend Development Team Lead

<!DOCTYPE html> <html dir="ltr" lang="en" class="no-js usn"> <head> <!-- Google tag (gtag.js) --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-76E7HJ89GV"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-76E7HJ89GV'); </script> <script type="text/javascript"> (function (c, l, a, r, i, t, y) { c[a] = c[a] || function () { (c[a].q = c[a].q || []).push(arguments) }; t = l.createElement(r); t.async = 1; t.src = "https://www.clarity.ms/tag/" + i; y = l.getElementsByTagName(r)[0]; y.parentNode.insertBefore(t, y); })(window, document, "clarity", "script", "l2bjwit4xj"); </script> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, maximum-scale=2"> <link rel="canonical" href="https://www.rachelbreeze.dev/blogs/utilizing-the-imiddleware-interface-in-umbraco/" /> <title>Utilizing the IMiddleware interface in Umbraco - Rachel Breeze, Backend Development Team Lead</title> <meta name="description" content="Discover how to use the IMiddleware interface in Umbraco for custom request processing and learn best practices for building efficient applications."> <link rel="alternate" hreflang="en-us" href="https://www.rachelbreeze.dev/blogs/utilizing-the-imiddleware-interface-in-umbraco/" /> <meta name="twitter:card" content="summary_large_image" /> <meta name="twitter:site" content="@BreezeRachel" /> <meta property="og:type" content="website"> <meta property="og:title" content="Utilizing the IMiddleware interface in Umbraco - Rachel Breeze, Backend Development Team Lead" /> <meta property="og:image" content="https://www.rachelbreeze.dev/media/kcwpp1h0/middleware.webp" /> <meta property="og:image:width" content="1024" /> <meta property="og:image:height" content="1024" /> <meta property="og:url" content="https://www.rachelbreeze.dev/blogs/utilizing-the-imiddleware-interface-in-umbraco/" /> <meta property="og:description" content="Discover how to use the IMiddleware interface in Umbraco for custom request processing and learn best practices for building efficient applications." /> <meta property="og:site_name" content="Rachel Breeze, Backend Development Team Lead" /> <meta name="robots" content="index,follow"> <script type="application/ld+json"> { "@context": "https://schema.org", "@type": "BlogPosting", "@id":"https://www.rachelbreeze.dev/blogs/utilizing-the-imiddleware-interface-in-umbraco/", "headline": "Utilizing the IMiddleware interface in Umbraco - Rachel Breeze, Backend Development Team Lead", "name":"Utilizing the IMiddleware interface in Umbraco - Rachel Breeze, Backend Development Team Lead", "description": "Discover how to use the IMiddleware interface in Umbraco for custom request processing and learn best practices for building efficient applications.", "datePublished" : "2024-12-30T00:00:00.000Z", "dateModified" : "2024-12-30T00:00:00.000Z", "mainEntityOfPage":"https://www.rachelbreeze.dev/blogs/utilizing-the-imiddleware-interface-in-umbraco/", "author": [ { "@type": "Person", "name": "Rachel Breeze", "url": "https://www.rachelbreeze.dev/about-me/" } ], "image": { "@type": "ImageObject", "@id": "https://www.rachelbreeze.dev/media/kcwpp1h0/middleware.webp", "url": "https://www.rachelbreeze.dev/media/kcwpp1h0/middleware.webp", "height": "1024", "width": "1024" }, "publisher": { "@type": "Person", "name": "Rachel Breeze", "url": "https://www.rachelbreeze.dev/about-me/" } } </script> <link href="/uSkinned/css/libraries/bootstrap.min1.css" media="screen" rel="stylesheet" /> <link href="/uSkinned/css/libraries/slick-carousel1.css" media="screen" rel="stylesheet" /><link href="/css/website/icons/icons1.css" media="screen" rel="stylesheet" /><link href="/css/website/base/theme1.css" media="screen" rel="stylesheet" /><link href="/css/website/base/plugins1.css" media="screen" rel="stylesheet" /><link href="/css/website/base/typography1.css" media="screen" rel="stylesheet" /><link href="/css/website/layouts/body1.css" media="screen" rel="stylesheet" /><link href="/css/website/layouts/header1.css" media="screen" rel="stylesheet" /><link href="/css/website/layouts/footer1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/all_nav1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_breadcrumb1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_burger1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_cta1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_main1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_secondary1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_sub1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_pagination1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_quick-links1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_sitemap1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_social1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_footer1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/all_pc1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_alerts1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_anchors1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_banner1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_codeblock1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_cta1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_data-list1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_forms1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_gallery1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_instagram-feed1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_grid1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_links1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_listing1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_pods1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_split1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_tabs-accordions1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_windows1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/sitewide-pods/all_swp1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/forms/all_forms1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/forms/form_contact1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/forms/form_login1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/forms/form_newsletter1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/forms/form_site-search1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/forms/form_loading1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/all_misc1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_authors1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_back-to-top1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_buttons1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_images1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_lazyframe-video1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_lightbox1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_links1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_meta1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_notification1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_overlay-icons1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_scroll-prompt1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_tables1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/misc/misc_directions1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/shop/all_shop1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/shop/shop_snipcart1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/shop/shop_ecwid1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/listings/all_listings1.css" media="screen" rel="stylesheet" /><link href="/css/website/modules/listings/listings_filter-form1.css" media="screen" rel="stylesheet" /> <link href="/css/website/layouts/body-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/layouts/header-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_cta-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_main-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_secondary-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/page-components/all_pc-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_anchors-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_banner-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_windows-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/misc/misc_back-to-top-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/misc/misc_notification-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/misc/misc_scroll-prompt-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/misc/misc_directions-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/shop/all_shop-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/shop/shop_snipcart-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/shop/shop_ecwid-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/listings/all_listings-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /><link href="/css/website/modules/listings/listings_filter-form-small1.css" media="screen and (min-width:0) and (max-width:991px)" rel="stylesheet" /> <link href="/css/website/layouts/body-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/layouts/header-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_burger-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_cta-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_main-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_secondary-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_social-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/navigation/nav_sub-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/page-components/all_pc-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_anchors-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_banner-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_data-list-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_links-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_split-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/page-components/pc_windows-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/forms/all_forms-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/forms/form_login-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/forms/form_newsletter-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/forms/form_site-search-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/misc/misc_back-to-top-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/misc/misc_notification-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/misc/misc_scroll-prompt-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/misc/misc_directions-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/shop/all_shop-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/shop/shop_snipcart-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/shop/shop_ecwid-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/listings/all_listings-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /><link href="/css/website/modules/listings/listings_filter-form-large1.css" media="screen and (min-width:992px)" rel="stylesheet" /> <link rel="stylesheet" media="screen" href="/uSkinned/css/generated/5b9492f6-0e77-4320-9dbd-ae16f337b367638465639238470000.css" /> <link href="/css/website/print1.css" media="print" rel="stylesheet" /> <link rel="shortcut icon" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="57x57" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="60x60" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="72x72" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="76x76" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="114x114" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="120x120" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="144x144" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="152x152" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="apple-touch-icon" sizes="180x180" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="icon" sizes="16x16" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="icon" sizes="32x32" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="icon" sizes="96x96" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <link rel="icon" sizes="192x192" type="image/ico" href="/media/5jtdzsvh/favicon.ico"> <meta name="msapplication-square70x70logo" content="/media/5jtdzsvh/favicon.ico" /> <meta name="msapplication-square150x150logo" content="/media/5jtdzsvh/favicon.ico" /> <meta name="msapplication-wide310x150logo" content="/media/5jtdzsvh/favicon.ico" /> <meta name="msapplication-square310x310logo" content="/media/5jtdzsvh/favicon.ico" /> <script type="text/javascript"> !function (T, l, y) { var S = T.location, k = "script", D = "instrumentationKey", C = "ingestionendpoint", I = "disableExceptionTracking", E = "ai.device.", b = "toLowerCase", w = "crossOrigin", N = "POST", e = "appInsightsSDK", t = y.name || "appInsights"; (y.name || T[e]) && (T[e] = t); var n = T[t] || function (d) { var g = !1, f = !1, m = { initialize: !0, queue: [], sv: "5", version: 2, config: d }; function v(e, t) { var n = {}, a = "Browser"; return n[E + "id"] = a[b](), n[E + "type"] = a, n["ai.operation.name"] = S && S.pathname || "_unknown_", n["ai.internal.sdkVersion"] = "javascript:snippet_" + (m.sv || m.version), { time: function () { var e = new Date; function t(e) { var t = "" + e; return 1 === t.length && (t = "0" + t), t } return e.getUTCFullYear() + "-" + t(1 + e.getUTCMonth()) + "-" + t(e.getUTCDate()) + "T" + t(e.getUTCHours()) + ":" + t(e.getUTCMinutes()) + ":" + t(e.getUTCSeconds()) + "." + ((e.getUTCMilliseconds() / 1e3).toFixed(3) + "").slice(2, 5) + "Z" }(), iKey: e, name: "Microsoft.ApplicationInsights." + e.replace(/-/g, "") + "." + t, sampleRate: 100, tags: n, data: { baseData: { ver: 2 } } } } var h = d.url || y.src; if (h) { function a(e) { var t, n, a, i, r, o, s, c, u, p, l; g = !0, m.queue = [], f || (f = !0, t = h, s = function () { var e = {}, t = d.connectionString; if (t) for (var n = t.split(";"), a = 0; a < n.length; a++) { var i = n[a].split("="); 2 === i.length && (e[i[0][b]()] = i[1]) } if (!e[C]) { var r = e.endpointsuffix, o = r ? e.location : null; e[C] = "https://" + (o ? o + "." : "") + "dc." + (r || "services.visualstudio.com") } return e }(), c = s[D] || d[D] || "", u = s[C], p = u ? u + "/v2/track" : d.endpointUrl, (l = []).push((n = "SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details)", a = t, i = p, (o = (r = v(c, "Exception")).data).baseType = "ExceptionData", o.baseData.exceptions = [{ typeName: "SDKLoadFailed", message: n.replace(/\./g, "-"), hasFullStack: !1, stack: n + "\nSnippet failed to load [" + a + "] -- Telemetry is disabled\nHelp Link: https://go.microsoft.com/fwlink/?linkid=2128109\nHost: " + (S && S.pathname || "_unknown_") + "\nEndpoint: " + i, parsedStack: [] }], r)), l.push(function (e, t, n, a) { var i = v(c, "Message"), r = i.data; r.baseType = "MessageData"; var o = r.baseData; return o.message = 'AI (Internal): 99 message:"' + ("SDK LOAD Failure: Failed to load Application Insights SDK script (See stack for details) (" + n + ")").replace(/\"/g, "") + '"', o.properties = { endpoint: a }, i }(0, 0, t, p)), function (e, t) { if (JSON) { var n = T.fetch; if (n && !y.useXhr) n(t, { method: N, body: JSON.stringify(e), mode: "cors" }); else if (XMLHttpRequest) { var a = new XMLHttpRequest; a.open(N, t), a.setRequestHeader("Content-type", "application/json"), a.send(JSON.stringify(e)) } } }(l, p)) } function i(e, t) { f || setTimeout(function () { !t && m.core || a() }, 500) } var e = function () { var n = l.createElement(k); n.src = h; var e = y[w]; return !e && "" !== e || "undefined" == n[w] || (n[w] = e), n.onload = i, n.onerror = a, n.onreadystatechange = function (e, t) { "loaded" !== n.readyState && "complete" !== n.readyState || i(0, t) }, n }(); y.ld < 0 ? l.getElementsByTagName("head")[0].appendChild(e) : setTimeout(function () { l.getElementsByTagName(k)[0].parentNode.appendChild(e) }, y.ld || 0) } try { m.cookie = l.cookie } catch (p) { } function t(e) { for (; e.length;)!function (t) { m[t] = function () { var e = arguments; g || m.queue.push(function () { m[t].apply(m, e) }) } }(e.pop()) } var n = "track", r = "TrackPage", o = "TrackEvent"; t([n + "Event", n + "PageView", n + "Exception", n + "Trace", n + "DependencyData", n + "Metric", n + "PageViewPerformance", "start" + r, "stop" + r, "start" + o, "stop" + o, "addTelemetryInitializer", "setAuthenticatedUserContext", "clearAuthenticatedUserContext", "flush"]), m.SeverityLevel = { Verbose: 0, Information: 1, Warning: 2, Error: 3, Critical: 4 }; var s = (d.extensionConfig || {}).ApplicationInsightsAnalytics || {}; if (!0 !== d[I] && !0 !== s[I]) { var c = "onerror"; t(["_" + c]); var u = T[c]; T[c] = function (e, t, n, a, i) { var r = u && u(e, t, n, a, i); return !0 !== r && m["_" + c]({ message: e, url: t, lineNumber: n, columnNumber: a, error: i }), r }, d.autoExceptionInstrumented = !0 } return m }(y.cfg); function a() { y.onInit && y.onInit(n) } (T[t] = n).queue && 0 === n.queue.length ? (n.queue.push(a), n.trackPageView({})) : a() }(window, document, { src: "https://js.monitor.azure.com/scripts/b/ai.2.min.js", // The SDK URL Source crossOrigin: "anonymous", cfg: { // Application Insights Configuration connectionString: 'InstrumentationKey=da3085e8-2f66-4ca9-827a-91f68ba294ce;IngestionEndpoint=https://uksouth-1.in.applicationinsights.azure.com/;LiveEndpoint=https://uksouth.livediagnostics.monitor.azure.com/;ApplicationId=c6f6b2e7-c8f4-4447-a757-d11afd47b4c1' } });</script> </head> <body class="body-bg body-bg-solid header-02-lg header-02-sm hide_header-on-scroll-lg show_header-on-scroll-sm directional-icons-chevron content-underline-link content-underline-link-hover breadcrumb-forward" > <!-- Skip links --> <a class="skip-to-content" id="skip-to-content-link" href="#site-content">Skip to main content</a> <a class="skip-to-content" id="skip-to-footer-link" href="#site-footer">Skip to footer</a> <!--// Skip links --> <!-- Site --> <div id="site"> <!-- Header --> <header id="site-header" class="header-bg header-bg-solid" tabindex="-1"> <div class="container-fluid"> <div class="row"> <div id="logo" > <a href="/" title="Rachel Breeze, Backend Development Team Lead"> <picture> <source type="image/webp" srcset="\media\w5ikmjmz/rb-high-resolution-color-logo--0x0x0xFalsewebp.png"> <img src="/media/w5ikmjmz/rb-high-resolution-color-logo.png" alt="Site Logo"> </picture> <span>Rachel Breeze, Backend Development Team Lead</span> </a> </div> <!-- Expand --> <button id="burgerNavStyle" class="expand-header expand-3-bars expand-uneven-lines expand-active-cross" type="button" aria-expanded="false" aria-label="Expand Menu"> <span>Menu</span> </button> <!--// Expand --> <!-- Main navigation --> <nav aria-label="Main" class="main nav-dropdown navigation-dropdown-bg navigation-dropdown-bg-solid"> <ul > <li class="no-child "> <span> <a href="/" >Home</a> </span> </li> <li class="no-child active"> <span> <a href="/blogs/" >Blogs</a> </span> </li> <li class="no-child "> <span> <a href="/speaking/" >Speaking</a> </span> </li> <li class="no-child "> <span> <a href="/series/" >Series</a> </span> </li> <li class="no-child "> <span> <a href="/community/" >Community</a> </span> </li> <li class="no-child "> <span> <a href="/about-me/" >About Me</a> </span> </li> </ul> </nav> <!--// Main navigation --> <!-- Secondary navigation --> <!--// Secondary navigation --> <!-- CTA Links --> <!--// CTA Links --> <!-- Site search --> <!--// Site search --> </div> </div> </header> <!--// Header --> <!-- Content --> <main id="site-content" tabindex="-1"> <!-- Breadcrumb --> <script type="application/ld+json"> { "@context": "https://schema.org/", "@type": "BreadcrumbList", "itemListElement": [ { "@type": "ListItem", "position": "1", "name": "Home", "item": "https://www.rachelbreeze.dev/" } ,{ "@type": "ListItem", "position": "2", "name": "Blogs", "item": "https://www.rachelbreeze.dev/blogs/" } ] } </script> <section class="breadcrumb-trail"> <div class="container"> <nav aria-label="Breadcrumb"> <ol class="breadcrumb"> <li class="breadcrumb-item"><a href="/"><i class="icon before"></i>Home</a></li> <li class="breadcrumb-item"><a href="/blogs/"><i class="icon before"></i>Blogs</a></li> <li class="breadcrumb-item " aria-current="page"><span><i class="icon before"></i>Utilizing the IMiddleware interface in Umbraco</span></li> </ol> </nav> </div> </section> <!--// Breadcrumb --> <section class="content component usn_cmp_banner base-bg base-bg-solid p-0 " > <div class="component-inner"> <div class="item item_short-banner c2-bg c2-bg-solid "> <div class="inner"> <div class="container"> <!-- ROW --> <div class="row text-left justify-content-center info_align-middle"> <div class="info col"> <div class="d-table"> <div class="d-table-cell"> <h1 class="heading c2-heading " data-os-animation="fadeIn" data-os-animation-delay="0s" data-os-animation-duration="1.2s" >Utilizing the IMiddleware interface in Umbraco</h1> </div> </div> </div> </div> <!--// ROW --> </div> </div> </div> </div> </section> <section class="content component usn_cmp_gridlayout base-bg base-bg-solid p-0 " > <div class="component-inner"> <div class="container"> <div class="" data-os-animation="fadeIn" data-os-animation-delay="0s" data-os-animation-duration="1.2s"> <div class="umb-grid"> <div class="grid-section"> <div > <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div > <h2>What Is Middleware</h2> <p>Middleware is software executed as part of an application's request and response processing pipeline.</p> <p>Middleware components are arranged in a pipeline and executed sequentially in the order they are registered. A middleware component can prevent further components in the pipeline from being called. Middleware allows developers to customise the behaviour of the request processing pipeline by adding or modifying components.</p> <p>Middleware supported out of the box by Microsoft includes support for HSTS, HttpsRedirection, Static Files and more. Examples of ways to extend this include support for caching, logging, image processing, header injection and permissions policies.</p> <h2>The IMiddleware Interface</h2> <p>The interface IMiddleware allows developers to customise the middleware for the app's request pipeline. It handles requests and responses through the request pipeline. From Microsoft:</p> <p><a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.imiddlewarefactory?wt.mc_id=MVP_329916" data-anchor="?wt.mc_id=MVP_329916" target="_blank" rel="noopener">IMiddlewareFactory</a>/<a href="https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.imiddleware?wt.mc_id=MVP_329916" data-anchor="?wt.mc_id=MVP_329916" target="_blank" rel="noopener">IMiddleware</a> is an extensibility point for <a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-8.0&amp;wt.mc_id=MVP_329916" target="_blank" data-anchor="?view=aspnetcore-8.0&amp;wt.mc_id=MVP_329916" rel="noopener">Middleware</a> activation that offers the following benefits:</p> <ul> <li>Activation per client request (injection of scoped services)</li> <li>Strong typing of middleware</li> </ul> <p>An example is shown below:</p> <code> public&nbsp;class&nbsp;MyMiddleware&nbsp;:&nbsp;IMiddleware<br>{<br>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;readonly&nbsp;IMyService&nbsp;_myService;<br>&nbsp;&nbsp;&nbsp;&nbsp;private&nbsp;readonly&nbsp;IUmbracoContextAccessor&nbsp;_umbracoContextAccessor;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;MyMiddleware(IMyService&nbsp;myService,&nbsp;IUmbracoContextAccessor&nbsp;umbracoContextAccessor)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_myService&nbsp;=&nbsp;myService;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_umbracoContextAccessor&nbsp;=&nbsp;umbracoContextAccessor;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br><br>&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;async&nbsp;Task&nbsp;InvokeAsync(HttpContext&nbsp;context,&nbsp;RequestDelegate&nbsp;next)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Your&nbsp;custom&nbsp;middleware&nbsp;logic&nbsp;here&nbsp;using&nbsp;_myService<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;...<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;await&nbsp;next(context);<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>} </code> <h2>Middleware Component Execution Order</h2> <p>The middleware pipeline is configured in <code class="code">Program.cs. </code>Components are executed in the order they are added to the pipeline; <span style="box-sizing: border-box; margin: 0px; padding: 0px;">Microsoft details the <a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-8.0&amp;wt.mc_id=MVP_329916#middleware-order" target="_blank" rel="noopener">middleware order in full</a></span>. The terminal middleware is Endpoint middleware; this is the last piece of middleware that will be executed before a response is sent to the client.</p> <p>This means:</p> <ul> <li>Ordering of custom middleware must consider the pipeline and the pipeline components that come before and after it.</li> <li>Middleware must be add calls prior to <code class="code">UseEndpoints()</code> method or, if developing with Umbraco, before <code class="code">.WithEndpoints()</code> method. </li> </ul> <h3>Umbraco</h3> <p>In Umbraco in  <code class="code">Program.cs</code> the Middleware components can be registered before <code class="code">app.UseUmbraco()</code> as follows:</p> <code> app.UseMiddleware&lt;MyMiddleware&gt;();<br><br>app.UseUmbraco()<br>&nbsp;&nbsp;&nbsp;&nbsp;.WithMiddleware(u&nbsp;=&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u.UseBackOffice();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u.UseWebsite();<br>&nbsp;&nbsp;&nbsp;&nbsp;})<br>&nbsp;&nbsp;&nbsp;&nbsp;.WithEndpoints(u&nbsp;=&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u.UseInstallerEndpoints();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u.UseBackOfficeEndpoints();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;u.UseWebsiteEndpoints();<br>&nbsp;&nbsp;&nbsp;&nbsp;}); </code> <p>For any MiddleWare routine that relies on the UmbracoPipeline consider using the <a href="https://docs.umbraco.com/umbraco-cms/reference/routing/custom-middleware" target="_blank" rel="noopener">Middleware extension points that Umbraco provides</a>.</p> <h3>Non-Umbraco Sites</h3> <p>For non-Umbraco Sites the middleware can be injected as follows into <code class="code">Program.cs</code>:</p> <code> app.UseMiddleware&lt;MyMiddleware&gt;();<br>&nbsp;&nbsp;&nbsp;&nbsp;app.UseRouting();<br>&nbsp;&nbsp;&nbsp;&nbsp;app.UseEndpoints(endpoints&nbsp;=&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;....<br>&nbsp;&nbsp;&nbsp;&nbsp;}); </code> <h2>Container Registration</h2> <p>Microsoft refers to the use of IMiddleware or IMiddlewareFactory as "factory-activated" middleware. For it to work correctly, it must be registered in the built-in container.</p> <h3>Umbraco</h3> <p>In Umbraco sites the<a href="https://docs.umbraco.com/umbraco-cms/implementation/composing" target="_blank" rel="noopener"> IComposer pattern</a> can be followed:</p> <code> using&nbsp;Microsoft.Extensions.DependencyInjection;<br>using&nbsp;Umbraco.Cms.Core.Composing;<br>using&nbsp;Umbraco.Cms.Core.DependencyInjection;<br><br>public&nbsp;class&nbsp;MyMiddlewareComposer:&nbsp;IComposer<br>{<br> public&nbsp;void&nbsp;Compose(IUmbracoBuilder&nbsp;builder)<br> {<br> builder.Services.AddTransient&lt;MyMiddleware&gt;();<br> }<br>} </code> <h3>Non-Umbraco Sites</h3> <p>Inject the middleware into the container, in <code class="code">Program.cs</code> as follows:</p> <code> var&nbsp;builder&nbsp;=&nbsp;WebApplication.CreateBuilder(args);<br><br>builder.Services.AddTransient&lt;FactoryActivatedMiddleware&gt;(); </code> <h2>Top Tips</h2> <ul> <li>Too much middleware will slow the application down.</li> <li>Be mindful of execution order when adding middleware components.</li> </ul> <p></p> <h2>Summary</h2> <p>The IMiddleware interface allows modification of incoming requests and outgoing responses. It creates an extension point for dependency injection/ inversion of control into Middleware. This enables the development of testable, robust applications.</p> <h2>References</h2> <ul> <li><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-8.0&amp;wt.mc_id=MVP_329916" data-anchor="?view=aspnetcore-8.0&amp;wt.mc_id=MVP_329916">ASP.NET Core Middleware</a></li> <li><a href="https://learn.microsoft.com/en-us/aspnet/core/fundamentals/middleware/extensibility?view=aspnetcore-8.0&amp;wt.mc_id=MVP_329916" data-anchor="?view=aspnetcore-8.0&amp;wt.mc_id=MVP_329916" target="_blank" rel="noopener">Factory-based middleware activation in ASP.NET Core</a></li> <li><a href="https://medium.com/@shubhadeepchat/net-core-middleware-explained-8c21bf646700" target="_blank" rel="noopener">.Net Core Middleware Explained</a></li> <li><a href="https://docs.umbraco.com/umbraco-cms/reference/using-ioc" target="_blank" rel="noopener">Inversion of Control / Dependency injection in Umbraco</a></li> </ul> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </section> <section class="content component meta-author-comments base-bg base-bg-solid pt-0 mt-0"> <div class="container"> <div class="row justify-content-center"> <div class="content-col center-col col"> <!-- Meta --> <section class="content component meta base-bg"> <p class="date base-text"><time>30 Dec 2024</time></p> </section> <!--// Meta --> </div> </div> </div> </section> </main> <!--// Content --> <!-- Footer --> <footer id="site-footer" class="footer-bg footer-bg-solid" tabindex="-1"> <div class="container"> <!-- Row --> <div class="row listing"> <div class="item footer-item usn_pod_sociallinks col-lg-3 col-md-3 col-12 col pt-xl-4 pt-lg-4"> <div class="inner"> <p class="heading sm footer-heading " data-os-animation="fadeInDown" data-os-animation-delay="0s" data-os-animation-duration="1s" >Follow me:</p> <nav class="social " data-os-animation="fadeInDown" data-os-animation-delay="0s" data-os-animation-duration="1s" aria-label="Social links"> <ul> <li> <span> <a href="https://www.linkedin.com/in/rachel-breeze/" target="_blank" rel="noreferrer noopener" title="Link will open in a new window/tab" > <picture> <source type="image/webp" data-srcset="\media\e04pcfcy/linkedin--0x80x0xFalsewebp.png 1x, \media\e04pcfcy/linkedin--0x160x0xFalsewebp.png 2x"> <img class="lazyload" src="\media\e04pcfcy/linkedin--0x16x0xFalsewebp.png" data-srcset="\media\e04pcfcy/linkedin--0x80x0xFalsewebp.png 1x, \media\e04pcfcy/linkedin--0x160x0xFalsewebp.png 2x" alt="Linkedin"> </picture> </a> </span> </li> <li> <span> <a href="https://twitter.com/BreezeRachel" target="_blank" rel="noreferrer noopener" title="Link will open in a new window/tab" > <picture> <source type="image/webp" data-srcset="\media\asei0c4p/download-1--0x80x0xFalsewebp.png 1x, \media\asei0c4p/download-1--0x160x0xFalsewebp.png 2x"> <img class="lazyload" src="\media\asei0c4p/download-1--0x16x0xFalsewebp.png" data-srcset="\media\asei0c4p/download-1--0x80x0xFalsewebp.png 1x, \media\asei0c4p/download-1--0x160x0xFalsewebp.png 2x" alt="X"> </picture> </a> </span> </li> <li> <span> <a href="https://www.threads.net/@rachel.breeze" target="_blank" rel="noreferrer noopener" title="Link will open in a new window/tab" > <picture> <source type="image/webp" data-srcset="\media\lbgkv2s0/download-2--0x80x0xFalsewebp.png 1x, \media\lbgkv2s0/download-2--0x160x0xFalsewebp.png 2x"> <img class="lazyload" src="\media\lbgkv2s0/download-2--0x16x0xFalsewebp.png" data-srcset="\media\lbgkv2s0/download-2--0x80x0xFalsewebp.png 1x, \media\lbgkv2s0/download-2--0x160x0xFalsewebp.png 2x" alt="Threads"> </picture> </a> </span> </li> <li> <span> <a href="https://github.com/RachBreeze" target="_blank" rel="noreferrer noopener" title="Link will open in a new window/tab" > <picture> <source type="image/webp" data-srcset="\media\cgfpgr2q/github-mark-white--0x80x0xFalsewebp.png 1x, \media\cgfpgr2q/github-mark-white--0x160x0xFalsewebp.png 2x"> <img class="lazyload" src="\media\cgfpgr2q/github-mark-white--0x16x0xFalsewebp.png" data-srcset="\media\cgfpgr2q/github-mark-white--0x80x0xFalsewebp.png 1x, \media\cgfpgr2q/github-mark-white--0x160x0xFalsewebp.png 2x" alt="GitHub"> </picture> </a> </span> </li> <li> <span> <a href="https://geekdom.social/@rachelbreezedev" target="_blank" rel="noreferrer noopener" title="Link will open in a new window/tab" > <picture> <source type="image/webp" data-srcset="\media\vmqdarza/mastodon--0x80x0xFalsewebp.png 1x, \media\vmqdarza/mastodon--0x160x0xFalsewebp.png 2x"> <img class="lazyload" src="\media\vmqdarza/mastodon--0x16x0xFalsewebp.png" data-srcset="\media\vmqdarza/mastodon--0x80x0xFalsewebp.png 1x, \media\vmqdarza/mastodon--0x160x0xFalsewebp.png 2x" alt="Mastodon"> </picture> </a> </span> </li> <li> <span> <a href="https://sessionize.com/rachel-breeze/" target="_blank" rel="noreferrer noopener" title="Link will open in a new window/tab" > <img src="/media/oh4deejg/sessionize-avatar.svg" alt="Sessionize"> </a> </span> </li> <li> <span> <a href="/blogs/rss.xml" > <picture> <source type="image/webp" data-srcset="\media\s5njfdnd/rss--0x80x0xFalsewebp.png 1x, \media\s5njfdnd/rss--0x160x0xFalsewebp.png 2x"> <img class="lazyload" src="\media\s5njfdnd/rss--0x16x0xFalsewebp.png" data-srcset="\media\s5njfdnd/rss--0x80x0xFalsewebp.png 1x, \media\s5njfdnd/rss--0x160x0xFalsewebp.png 2x" alt="RSS"> </picture> </a> </span> </li> <li> <span> <a href="https://bsky.app/profile/rachelbeezedev.bsky.social" target="_blank" rel="noreferrer noopener" title="Link will open in a new window/tab" > <picture> <source type="image/webp" data-srcset="\media\24lhwd4h/bluesky--0x80x0xFalsewebp.png 1x, \media\24lhwd4h/bluesky--0x160x0xFalsewebp.png 2x"> <img class="lazyload" src="\media\24lhwd4h/bluesky--0x16x0xFalsewebp.png" data-srcset="\media\24lhwd4h/bluesky--0x80x0xFalsewebp.png 1x, \media\24lhwd4h/bluesky--0x160x0xFalsewebp.png 2x" alt="Blue Sky"> </picture> </a> </span> </li> </ul> </nav> </div> </div> <div class="item footer-item col-lg-3 col-md-3 col-12 col item_text-below usn_pod_textimage text-left pt-xl-4 pt-lg-4 " data-os-animation="fadeIn" data-os-animation-delay="0s" data-os-animation-duration="1.2s"> <div class="inner "> <div class="info "> <p class="heading sm footer-heading " >Email me</p> <div class="text footer-text"> <p><a href="mailto:hello@rachelbreeze.dev">hello@rachelbreeze.dev</a></p> </div> </div> </div> </div> </div> <!--// Row --> <!-- Row --> <div class="row"> <div class="col"> <nav aria-label="Footer" class="footer-navigation"> <ul> <li><div>&copy; 2024 Rachel Breeze</div></li> </ul> </nav> </div> </div> <!--// Row --> </div> </footer> <!--// Footer --> </div> <!--// Site --> <script src="/uSkinned/scripts/libraries/jquery.min1.js" type="text/javascript"></script><script src="/uSkinned/scripts/libraries/popper.min1.js" type="text/javascript"></script><script src="/uSkinned/scripts/libraries/bootstrap.min1.js" type="text/javascript"></script><script src="/uSkinned/scripts/libraries/jquery.validate.min1.js" type="text/javascript"></script><script src="/uSkinned/scripts/libraries/jquery.validation-unobtrusive.min1.js" type="text/javascript"></script><script src="/uSkinned/scripts/libraries/slick-carousel1.js" type="text/javascript"></script><script src="/uSkinned/scripts/libraries/bootstrap.lightbox1.js" type="text/javascript"></script><script src="/scripts/website/plugins1.js" type="text/javascript"></script><script src="/scripts/website/application1.js" type="text/javascript"></script><script src="/uSkinned/scripts/libraries/jquery.unobtrusive-ajax1.js" type="text/javascript"></script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10