How to display errors with ASP.NET Core

I have a fairly simple website that I am playing with using ASP.NET Core. I am running the application from the command line and the website is returning static files but I keep getting 500 errors when I attempt to make a request that should get handled by MVC. How do I see what the error is? Whether the error is displayed to the browser or logged to the console doesn't matter I just want a way to see what the error is.


Solution 1:

Add the error page middleware as shown here:

app.UseDeveloperExceptionPage();

Solution 2:

Update for beta8:
In beta8 Microsoft changed the name to UseDeveloperExceptionPage. So if you want to use the ErrorPage, call:

app.UseDeveloperExceptionPage();

Here is the link to the related Github issue.
The ErrorPageOptions are the same as in beta6/7.


You can use

app.UseErrorPage(ErrorPageOptions.ShowAll)

until beta5 of Asp.Net Mvc.


As of beta6, ErrorPageOptions.ShowAll has been removed. You can now use the version without parameters

app.UseErrorPage();

or create an ErrorPageOptions object and specify how many lines around the error you want to display by setting SourceCodeLineCount.

app.UseErrorPage(new ErrorPageOptions() {SourceCodeLineCount = 100});

Additional Information

They removed multiple properties of ErrorPageOptions in this commit.

Before:

public class ErrorPageOptions
{
    private bool _defaultVisibility;

    private bool? _showExceptionDetails;
    private bool? _showSourceCode;
    private bool? _showQuery;
    private bool? _showCookies;
    private bool? _showHeaders;
    private bool? _showEnvironment;
    ...
}

After:

public class ErrorPageOptions
{
    public int SourceCodeLineCount { get; set; }
    public IFileProvider FileProvider { get; set; }
    ...
}

So now you can only set how many lines of source code are printed.

Solution 3:

If you don't care that your error details would be exposed to the world, you can enable the error details, right in the browser without any code changes. (This was only tested in IIS 8.5):

  • In IIS Manager, in the left Connections section, left-click select your Site.
  • In the right side Feature View open Error Pages.
  • On the far right Actions section, click on Edit Feature Settings
  • In the Error Responses, select the 2nd, Detailed errors, option then Ok (or if you are worried about exposing stuff to the world, start with the 3rd option, if you can open a local browser... ie, localhost:...)

This should be enough for you to be able to see the exact error... Important: If you had to use the middle Detailed errors option, be sure to turn it off once you debug the problem. This can give a hacker all he needs to break into your server.