ASP.NET Core MVC Interview Question #137

How should global exception handling be configured in an MVC application?

Dependency Injection, Services, Error Handling, Logging & Performance Senior Advanced

Quick Interview Answer

Unexpected exceptions are normally handled centrally rather than with repetitive try/catch blocks in every action. ASP.NET Core can use exception-handling middleware to catch failures that reach it and execute an error-handling path.

Detailed Explanation

Unexpected exceptions are normally handled centrally rather than with repetitive try/catch blocks in every action. ASP.NET Core can use exception-handling middleware to catch failures that reach it and execute an error-handling path.

In production, error pages should be user-friendly and must not expose stack traces, connection strings, internal paths, or sensitive exception details. Detailed developer exception pages should be limited to development environments.

Code Example

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}