How to Turn on Detailed Exceptions in CircuitOptions.DetailedErrors with Blazor

While debugging an issue using JavaScript to call a C# class, I ran into an error in my web browser console.

Error: There was an exception invoking 'MethodName' on assembly 'AssemblyName'. For more details turn on detailed exceptions in 'CircuitOptions.DetailedErrors'.

Documentation appears to be sparse on how to enable these. Fortunate for us, it only takes a few lines of code.

In your Startup.cs file you'll need to add an IWebHostEnvironment property to the Startup class. You'll also need to adjust the constructor to accept one.

//Startup.cs
public class Startup
{
  private readonly IWebHostEnvironment _env;

  public Startup(IConfiguration configuration, IWebHostEnvironment env)
  {
      Configuration = configuration;
      _env = env;
  }
  //.....
}

This allows us to add a security check to show detailed exceptions only in a development environment.

Next add the Circuit Options to the ConfigureServices method that already exists.

//Startup.cs
//.....
public void ConfigureServices(IServiceCollection services)
{
  services.AddRazorPages();
  services.AddServerSideBlazor()
    .AddCircuitOptions(opt =>
    {
      opt.DetailedErrors = _env.IsDevelopment();
    });
}
//.....

Now the console in your web browser should give you a better error message!

One Last Thing...

If you have a question or see a mistake, please comment below.

If you found this post helpful, please share it with others. It's the best thanks I can ask for & it gives me momentum to keep writing!

Matt Ferderer
Software Developer focused on making great user experiences. I enjoy learning, sharing & helping others make amazing things.
Let's Connect