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();elseapp.UseExceptionHandler("/Error");
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.
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.
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.
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();
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.
```csharpusing 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 productionapp.UseExceptionHandler("/Error");}}}```
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.
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();});