BlazorWASM:生产环境下的详细错误
创始人
2024-12-21 22:30:19
0

当在生产环境中使用Blazor WASM时,您可能会遇到以下错误消息:'An unhandled exception occurred while processing the request.” 这个错误消息并不是非常有帮助,因为它没有提供任何关于错误原因的详细信息。但是,您可以使用以下步骤来获取Blazor WASM应用程序中出现问题的更具体的信息。

1.在页面上添加以下代码:

@if (isDevelopment) {

@Json.Serialize(error, new JsonSerializerOptions { WriteIndented = true })
}

2.在App.razor文件中创建一个isDevelopment属性,并在OnInitialized方法中设置它的值:

@code { private bool isDevelopment;

protected override void OnInitialized()
{
    isDevelopment = !String.Equals(env.EnvironmentName, "production", StringComparison.OrdinalIgnoreCase);
}

}

3.在Startup.cs文件中添加以下代码:

app.UseExceptionHandler("/Error");

app.UseMiddleware();

4.在BlazorProductionExceptionHandlerMiddleware.cs 文件中添加以下代码:

app.UseExceptionHandler("/Error");

app.UseMiddleware();

代码:

using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text.Json; using System.Threading.Tasks;

namespace BlazorApp1.Server.Middleware { public class BlazorProductionExceptionHandlerMiddleware { public async Task InvokeAsync(HttpContext context) { var exceptionHandlerPathFeature = context.Features.Get();

        if (exceptionHandlerPathFeature?.Error != null)
        {
            context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            context.Response.ContentType = "application/json";
            
            var error = new
            {
                exceptionHandlerPathFeature.Error.Message,
                exceptionHandlerPathFeature.Error.StackTrace,
                InnerException = exceptionHandlerPathFeature.Error.InnerException != null ? new
                {
                    exceptionHandlerPathFeature.Error.InnerException.Message,
                    exceptionHandlerPathFeature.Error.InnerException.StackTrace
                } : null
            };

            await context.Response.WriteAsync(JsonSerializer.Serialize(error));
        }
    }
}

// Extension method used to add the middleware to the HTTP request pipeline.
public static class BlazorProductionExceptionHandlerMiddlewareExtensions
{
    public static IApplicationBuilder UseBlazorProductionExceptionHandler(
        this IApplicationBuilder builder)
    {
        return builder.UseMiddleware();
    }
}

}

5.在Error.razor文件中添加以下代码:

@if (Error != null) { @if (IsDevelopment) {

相关内容

热门资讯

第五分钟了解“大唐撸麻雀辅助挂... 第五分钟了解“大唐撸麻雀辅助挂码”详细开挂辅助教程-竟然真的有挂1、许多玩家不知道大唐撸麻雀辅助挂码...
6分钟了解“欢乐茶馆辅助”详细... 6分钟了解“欢乐茶馆辅助”详细开挂辅助攻略-总是是真的挂1、操作简单,无需注册,只需要使用手机进行登...
3分钟了解“四川途游小程序辅助... 3分钟了解“四川途游小程序辅助”详细开挂辅助插件-确实是有挂四川途游小程序辅助辅助器中分为三种模型:...
五分钟了解“欢乐联盟辅助器”详... 五分钟了解“欢乐联盟辅助器”详细开挂辅助软件-切实真的有挂亲,关键说明,欢乐联盟辅助器赛季回归,欢乐...
1分钟了解“洞庭茶苑app辅助... 1分钟了解“洞庭茶苑app辅助”详细开挂辅助软件-果然是真的挂1、全新机制【洞庭茶苑app辅助软件透...
第一分钟了解“椰岛游戏破解版”... 第一分钟了解“椰岛游戏破解版”详细开挂辅助插件-其实是真的挂1、椰岛游戏破解版系统规律教程、椰岛游戏...
一分钟了解“菠萝辅助器免费版的... 一分钟了解“菠萝辅助器免费版的功能介绍”详细开挂辅助工具-一直有挂1、菠萝辅助器免费版的功能介绍ai...
八分钟了解“天蝎大厅辅助”详细... 八分钟了解“天蝎大厅辅助”详细开挂辅助技巧-总是是有挂所有人都在同一条线上,像星星一样排成一排,每一...
第2分钟了解“潮汕透视辅助”详... 第2分钟了解“潮汕透视辅助”详细开挂辅助方法-都是是有挂1、点击下载安装,潮汕透视辅助插件透视分类一...
第七分钟了解“哥哥打大a辅助”... 第七分钟了解“哥哥打大a辅助”详细开挂辅助插件-果然真的有挂1、哥哥打大a辅助系统规律教程、哥哥打大...