0% found this document useful (0 votes)
33 views38 pages

CH 3

Uploaded by

kbeydoun1980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views38 pages

CH 3

Uploaded by

kbeydoun1980
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

ASP.

Net Core –Handling requests


with the middleware pipeline
Kamal Beydoun
Lebanese University – Faculty of Sciences I
[email protected]
Middleware pipeline
The middleware pipeline is one of the most important parts of
configuration for defining how your application behaves and how it
responds to requests.
Understanding how to build and compose middleware is key to adding
functionality to your applications.

2
What is middleware?
 In ASP.NET Core, middleware is C# classes that can handle an HTTP
request or response.
Middleware can:
 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.
3
Pipeline

This arrangement, where a


piece of middleware can call
another piece of middleware,
which in turn can call another,
and so on, is referred to as a
pipeline.

4
Example

A key point to glean from this is


that the pipeline is bidirectional.

5
HttpContext object
The ASP.NET Core web server constructs an HttpContext, which the
ASP.NET Core application uses as a sort of storage box for a single
request.
 details of the original HTTP request and other configuration details
All middleware has access to the HttpContext for a request.

6
Combining middleware in a pipeline
How to create a middleware pipeline by composing small
components together ??
Using standard middleware components
create a holding page
serve static files from a folder on disk.

7
WelcomePageMiddleware
• WelcomePageMiddleware
is designed to quickly
provide a sample page when
you’re first developing an
application.

8
Startup.cs
• Configure method of Startup by adding middleware to an
IApplicationBuilder object.

9
10
11
Handling static files
Most web applications, including those with dynamic content,
serve a number of pages using static files.
Images, JavaScript, and CSS stylesheets are normally saved to disk
during development and are served up when requested, normally as
part of a full HTML page request.

12
In this example, an image called moon.jpg exists in the
wwwroot folder. When you request the file using the /moon.jpg
path, it’s loaded and returned as the response to the request. .
13
`

• If the user requests a file that doesn’t exist in the wwwroot folder
• a 404 HTTP error code response will be sent to the user’s browser, which will show its default “File
Not Found” page
14
15
• If the file doesn’t exist, then the request effectively passes through the static file middleware unchanged.
• ASP.NET Core effectively adds an automatic “dummy” piece of middleware to the end of the pipeline.
• This middleware always returns a 404 response if it’s called.
16
A Razor Pages application

17
Startup.cs

18
19
20
21
Handling errors using middleware
If an exception NullReferenceException
occurs in a middleware component, it
propagates up the pipeline.
 If the pipeline doesn’t handle the
exception, the web server will return a
500 status code back to the user.

22
Handling errors using middleware
Error handling middleware attempts to address these
problems by modifying the response before the app returns it to
the user.
returns details of the error that occurred
returns a generic, but friendly, HTML page to the user.
Place error handling middleware early in the middleware
pipeline to ensure it will catch any errors generated in
subsequent middleware 23
Handling errors using middleware

24
Viewing exceptions in development:
DeveloperExceptionPage
• Microsoft provides
DeveloperExceptionPage
Middleware, which can be
added to your middleware
pipeline using
app.UseDeveloperExcepti
onPage();

25
Handling exceptions in production:
ExceptionHandlerMiddleware

26
Handling exceptions

27
Customizing Error Page
• When adding ExceptionHandlerMiddleware to your application, you’ll typically
provide a path to the custom error page that will be displayed to the user.
app.UseExceptionHandler("/Error");
• ExceptionHandlerMiddleware will invoke this path after it captures an
exception, in order to generate the final response.
• The ability to dynamically generate a response is a key feature of
ExceptionHandlerMiddleware—it allows you to re-execute a middleware pipeline
in order to generate the response sent to the user.

28
29
StatusCodePagesMiddleware
o Microsoft provides
StatusCodePagesMiddleware for
handling this use case.
o As with all error handling middleware,
you should add it early in your
middleware pipeline, as it will only handle
errors generated by later middleware
components.

30
StatusCodePagesMiddleware

app.UseStatusCodePages();

31
StatusCodePagesMiddleware
• to re-execute the pipeline when an error is captured, replace the call to
UseStatusCodePages with the following extension method
app.UseStatusCodePagesWithReExecute("/{0}");
• Note that the error handling path "/{0}" contains a format string token,
{0}. When the path is re-executed, the middleware will replace this token
with the status code number. For example, a 404 error would re-execute
the /404 path.

32
33
34
Summary
• Middleware has a similar role to HTTP modules and handlers in ASP.NET
but is more easily reasoned about.
• Middleware is composed in a pipeline, with the output of one middleware
passing to the input of the next.
• The middleware pipeline is two-way: requests pass through each
middleware on the way in and responses pass back through in the reverse
order on the way out.

35
Summary
• Middleware can short-circuit the pipeline by handling a request and returning a response, or it can
pass the request on to the next middleware in the pipeline.
• Middleware can modify a request by adding data to, or changing, the HttpContext object.
• If an earlier middleware short-circuits the pipeline, not all middleware will execute for all requests.
• If a request isn’t handled, the middleware pipeline will return a 404 status code.
• The order in which middleware is added to IApplicationBuilder defines the order in which
middleware will execute in the pipeline.

36
Summary
• The middleware pipeline can be re-executed, as long as a response’s
headers haven’t been sent.
• When added to a middleware pipeline, StaticFileMiddleware will serve
any requested files found in the wwwroot folder of your application.
• DeveloperExceptionPageMiddleware provides a lot of information about
errors when developing an application but should never be used in
production.

37
Summary
• ExceptionHandlerMiddleware lets you provide user-friendly custom error
handling messages when an exception occurs in the pipeline.
• StatusCodePagesMiddleware lets you provide user-friendly custom error
handling messages when the pipeline returns a raw error response status
code.
• Microsoft provides some common middleware and there are many third-
party options available on NuGet and GitHub.

38

You might also like