MVC
Christen Zarif
outline
• Middleware
Middleware
https://www.tutorialspoint.com/asp.
net_core/asp.net_core_middleware.
htm
Middleware in Asp.Net Core
• The request handling pipeline is composed as a
series of middleware components.
• Each Component in the pipeline is a request
delegate
• Each component/delegate performs asynchronous
operations on an HttpContext and then either
invokes the next middleware in the pipeline or
terminates the request
Middleware in Asp.Net Core
(Con.)
• ASP.NET Core includes a rich set of built-in
middleware, and you can write custom middleware.
• A middleware component may handle the
request and decide not to call the next
middleware in the pipeline ,and returning
backup the call chain.
– This is called short-circuiting the request pipeline,.
Middleware
• A middleware is nothing but a component (class)
which is executed on every request in ASP.NET
Core application.
• There will be multiple middleware in ASP.NET Core
web application. It can be either framework
provided middleware, added via NuGet or your own
custom middleware.
• Each middleware adds or modifies http request and
optionally passes control to the next middleware
component.
Middleware in Asp.Net Core
• Has access to both Request and Response
• May Simply pass the Request to Next Middleware
• May process and then pass the Request to next
Midddleware
• May handle the Request and short-circuit the pipeline
• May process the outgoing Response
• Middlewares are executed in the order they are added
Built in Middelware
Built in Middleware (Con.)
• For More Details
ASP.NET Core Request Processin
• Middlewares build the request pipeline
Built in Middleware
Middleware order
• The order that middleware components are added in the
Startup.Configure method defines the order in which the
middleware components are invoked on requests and the
reverse order for the response. The order is critical for
security, performance, and functionality.
• On the other hand, if you are developing a secure data
driven web application then you may need several
middleware components like:
– StaticFiles middleware,
– Authentication middleware,
– Authorization middleware,
– MVC middleware etc.
Enabling Exception Handling
• Exception handling is one of the most
important features of any application.
• ASP.NET Core includes a Middleware that
makes exception handling easy.
• The Microsoft.AspNetCore.Diagnostics
package includes following extension
methods to handle exceptions in different
scenario:
– UseDeveloperExceptionPage :
– UseExceptionHandler
Serving Static Files
• Static files such as html, JavaScript, CSS, or image files
• By default, all the static files of a web application should
be located in the web root folder wwwroot.
• ASP.NET Core application cannot serve static files by
default. We must include
Microsoft.AspNetCore.StaticFiles middleware in the
request pipeline.
• The app.UseStaticFiles() method adds StaticFiles
middleware into the request pipeline.
Serving Static Files
• The UseDefaultFiles configures the DefaultFiles
middleware
which is a part of StaticFiles middleware.
This will automatically serve html file named
default.html,
default.htm, index.html or index.htm on the http
request http://localhost:<port>. Should be Added
before UseStaticFiles
• FileServer : middleware combines the
functionalities of UseDefaultFiles and
UseStaticFiles middlware.
UseRoute & USeEndpoint
• UseRouting: Matches request to an endpoint.
• UseEndpoints: Execute the matched endpoint.
• Types of Endpoints:
– MVC
– Razor Pages
– SignalR
Thank You