受欢迎的博客标签

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

Published

Table of Content

.Net 5.x

.Net 3.x

 

.Net 5.x

I'm getting this message in the console when running a Blazor Server app:

An error has occurred. This application may no longer respond until reloaded. Reload

An unhandled exception has occurred. See browser dev tools for details.Reload

This article will allow you to see the errors in the browser console with ASP.NET Core Blazor 5.x(Server-side).

 

Blazor error handling documentation

https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/handle-errors?view=aspnetcore-5.0

 

1.How to Turn on Blazor Server detailed circuit errors 

You will getting this message in the browser console when running a server-side Blazor app:

An error has occurred. This application may no longer respond until reloaded. Reload

step 1:Add an IWebHostEnvironment property

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
{
        -public Startup(IConfiguration configuration)
        +public Startup(IConfiguration configuration, IWebHostEnvironment env)
        {
            Configuration = configuration;
           + _env = env;
        }

        public IConfiguration Configuration { get; }
        +private readonly IWebHostEnvironment _env;
 
}

Step 2:Add the Circuit Options

Next add the Circuit Options to the ConfigureServices method that already exists.This change will allow you to see the errors in the browser console.

//Startup.cs

public void ConfigureServices(IServiceCollection services)
{
  services.AddRazorPages();
   services.AddServerSideBlazor()
                .AddCircuitOptions(options =>
                {
                    

                    if (_env.IsDevelopment()) //only add details when debugging
                    {
                        options.DetailedErrors = true;
                    }
                });
}

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

[2019-12-14T15:51:13.199Z] Error: System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
   at Microsoft.AspNetCore.Components.HttpClientJsonExtensions.GetJsonAsync[T](HttpClient httpClient, String requestUri)
   at Stockso.BlazorServer.Pages.WebAPI.v1.StockDataPhysicalDiskPanKouAggregateAuctionSearch.OnInitializedAsync() in x\src\BlazorServer\Stockso.BlazorServer\Pages\WebAPI\v1\StockDataPhysicalDiskPanKouAggregateAuctionSearch.razor:line 172
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

 

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

https://mattferderer.com/detailed-exceptions-circuitoptions-blazor

 

 

.Net core 3.x

I'm getting this message in the console when running a server-side Blazor app:

Error: There was an unhandled exception on the current circuit, so this circuit will be terminated. For more details turn on detailed exceptions in 'CircuitOptions.DetailedErrors'

This article will allow you to see the errors in the browser console with ASP.NET Core Blazor 3.x(Server-side).

 

Blazor error handling documentation

https://docs.microsoft.com/en-us/aspnet/core/blazor/handle-errors?view=aspnetcore-3.0

 

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

You will getting this message in the browser console when running a server-side Blazor app:

Error: There was an unhandled exception on the current circuit, so this circuit will be terminated. For more details turn on detailed exceptions in 'CircuitOptions.DetailedErrors'

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;
  }
 
}

 

Next add the Circuit Options to the ConfigureServices method that already exists.This change will allow you to see the errors in the browser console.

//Startup.cs

public void ConfigureServices(IServiceCollection services)
{
  services.AddRazorPages();
   services.AddServerSideBlazor()
                .AddCircuitOptions(options =>
                {
                    

                    if (_env.IsDevelopment()) //only add details when debugging
                    {
                        options.DetailedErrors = true;
                    }
                });
}

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

[2019-12-14T15:51:13.199Z] Error: System.Net.Http.HttpRequestException: Response status code does not indicate success: 404 (Not Found).
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()
   at System.Net.Http.HttpClient.GetStringAsyncCore(Task`1 getTask)
   at Microsoft.AspNetCore.Components.HttpClientJsonExtensions.GetJsonAsync[T](HttpClient httpClient, String requestUri)
   at Stockso.BlazorServer.Pages.WebAPI.v1.StockDataPhysicalDiskPanKouAggregateAuctionSearch.OnInitializedAsync() in x\src\BlazorServer\Stockso.BlazorServer\Pages\WebAPI\v1\StockDataPhysicalDiskPanKouAggregateAuctionSearch.razor:line 172
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

 

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

https://mattferderer.com/detailed-exceptions-circuitoptions-blazor