Creating a NotFound / 404 rule within ASP.NET Core is quite simple, and once you've created that you'll be able to create custom error pages for all error status codes.
1. Update the Startup.cs Located at the root of any ASP.NET Core application is the Startup.cs file containing all the necessary bootstrap code.
2. Update the Configure method with the following:
28 lines
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { //redirects all errors given their error code app.UseStatusCodePagesWithReExecute("/error/{0}"); app.UseApplicationInsightsExceptionTelemetry(); } ... }
The above code will now redirect all errors, e.g. a 404 not found error will get redirected to /error/404. Now we need to setup the routes and controllers to handle these requests.
3. Update the routing rules
We now need a routing rule to handle all the potential error redirects.