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!

Matt Ferderer

Full Stack Software Developer focused on JavaScript and C#. Love to share & help others make amazing things. Have a project you need help on? Let's talk!

comment

Comments