Custom Error Pages in .Net Core

1. Enable Custom Error Pages in Startup.cs
Configure Custom Error Handling Middleware
In your Startup.cs
file, configure the error handling middleware to show custom error pages for different status codes (e.g., 404, 500). You’ll typically use the UseStatusCodePagesWithReExecute
middleware for custom error pages.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage(); // Detailed error in development
}
else
{
// Custom error pages for non-development environments
app.UseExceptionHandler("/Error/GeneralError");
app.UseStatusCodePagesWithReExecute("/Error/{0}");
}
app.UseStaticFiles(); // Serve static files
app.UseMvcWithDefaultRoute(); // MVC routing
}
Custom Error Controller
Create an ErrorController
that will handle different error types. The {0}
in the path passed to UseStatusCodePagesWithReExecute
corresponds to the HTTP status code, such as 404 or 500.
public class ErrorController : Controller
{
public IActionResult GeneralError()
{
return View("GeneralError");
}
public IActionResult Error404()
{
return View("404");
}
public IActionResult Error500()
{
return View("500");
}
}
Create the corresponding views under Views/Error
for each error page (e.g., 404.cshtml
, 500.cshtml
, and GeneralError.cshtml
).
Example Folder Structure
/Views/Error/
├── 404.cshtml
├── 500.cshtml
└── GeneralError.cshtml
Custom Error Handling in appsettings.json (Optional)
You can also configure error settings like showing friendly error pages in appsettings.json
:
{
"CustomErrors": {
"Enabled": true,
"DefaultRedirect": "/Error/GeneralError"
}
}
In Startup.cs
, you can read this configuration and set the error pages accordingly.
2. Handle Static File Errors
To ensure custom error pages are shown for static file errors (e.g., missing images or CSS files), you can use the UseStatusCodePagesWithReExecute
middleware to catch 404 errors caused by static files as well.
app.UseStatusCodePagesWithReExecute("/Error/{0}");
This will redirect to the appropriate error page based on the HTTP status code, including those caused by static file errors.
Tip: Detailed Error Handling in Development
For local development, you can use the UseDeveloperExceptionPage()
to show detailed error information, and in production, use UseExceptionHandler()
with a custom error page.
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage(); // Show detailed error pages in development
}
else
{
app.UseExceptionHandler("/Error/GeneralError"); // Show a general error page in production
}
Summary
Task | Configuration Section |
---|---|
Handle ASP.NET runtime errors | app.UseExceptionHandler("/Error/GeneralError") |
Handle static file errors | app.UseStatusCodePagesWithReExecute("/Error/{0}") |
Show detailed errors locally | app.UseDeveloperExceptionPage() |
Fallback for all unhandled errors | app.UseExceptionHandler("/Error/GeneralError") |
So to clarify:
Feature | .NET Framework | .NET Core / ASP.NET Core |
---|---|---|
Custom error pages |
web.config ( , ) |
Startup.cs (UseExceptionHandler , UseStatusCodePagesWithReExecute ) |
Static file error handling |
in web.config
|
Handled via UseStatusCodePagesWithReExecute()
|
Detailed local errors | customErrors mode="RemoteOnly" |
env.IsDevelopment() + UseDeveloperExceptionPage()
|