Codecademy Logo

ASP.NET Middleware In Depth

Related learning

  • Learn how to build fast, secure, and maintainable web apps with ASP.NET and the Razor Pages architecture.
    • With Certificate
    • Intermediate.
      17 hours
  • Jumpstart your career with this skill path, first by learning the C# language, then building web apps with ASP.NET Core and the Razor Pages.
    • Includes 7 Courses
    • With Certificate
    • Intermediate.
      41 hours

Understanding ASP.NET UseExceptionHandler Component

The UseExceptionHandler() component can be used in production environments to catch and log errors and route users to a general error page without exposing sensitive details about the application. The UseExceptionHandler() component has several overloads, but a simple way of using it is to pass in the name of the page (as a string) that should display if an exception is thrown.

if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
else
app.UseExceptionHandler("/Error");

Redirecting Requests with ASP.NET Middleware

The UseHttpsRedirection() method is the middleware component used to capture HTTP requests and redirect them to the more secure HTTPS protocol. Since the redirection is done in the app configuration, the user never even has to know the redirection occurred.

Understanding the ASP.NET UseStaticFiles Component

Static files make up an important part of the application — they often contain HTML, CSS, and JavaScript code that controls how the application looks and behaves. The UseStaticFiles() method is available to ensure static file content is rendered alongside the HTML for our web applications.

Understanding the ASP.NET UseAuthorization Component

The UseAuthorization() component checks the user’s request against their authorization status. If the authorization check passes, this component will pass the request to the next component in the pipeline. Otherwise, it will short-circuit the pipeline and either present the user with a login page or an error.

Razor Pages Setup

In ASP.NET, Razor Pages endpoints are initialized via UseRouting() and MapRazorPages(). These methods configure the routing middleware to handle HTTP requests using Razor Pages, a framework for building dynamic web applications. Check out the sample code to see how to implement these in an ASP.NET Core 8 application.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();

Developer Exception Page

In ASP.NET Core 8, the UseDeveloperExceptionPage() middleware is automatically part of the pipeline and shows detailed errors during development. Remember to disable it in production to maintain security and prevent exposure of sensitive information.

```csharp
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
public class Startup
{
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// Add error handling for production
app.UseExceptionHandler("/Error");
}
}
}
```

Understanding ASP.NET Developer Exception Pages

UseDeveloperExceptionPage() is a method which provides an exception page specifically designed for developers. The method should be placed before any middleware components that are catching exceptions. Some of the information displayed includes the stack trace, any query string parameters, cookies, headers, and routing information.

Example developer exception page: InvalidOperationException

Enabling ASP.NET Endpoints

UseRouting(), must be called to compare the HTTP request with the available endpoints and decide which is the best match. UseEndpoints() then calls MapRazorPages() with a lambda expression to register the endpoint, and then executes the selected delegate that matches our HTTP request.

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

Learn more on Codecademy

  • Learn how to build fast, secure, and maintainable web apps with ASP.NET and the Razor Pages architecture.
    • With Certificate
    • Intermediate.
      17 hours
  • Jumpstart your career with this skill path, first by learning the C# language, then building web apps with ASP.NET Core and the Razor Pages.
    • Includes 7 Courses
    • With Certificate
    • Intermediate.
      41 hours