MVC6:UseDeveloperExceptionPage 在控制器中抛出的异常中不起作用

MVC6 : UseDeveloperExceptionPage not working from exception thrown in controllers

提问人:Neal Rogers 提问时间:7/19/2023 最后编辑:Neal Rogers 更新时间:7/31/2023 访问量:48

问:

我正在尝试在我的 中实现,但是如果我在控制器中抛出异常,错误页面不会显示(但是如果我从 Startup.cs 中的 Configure 方法中抛出异常,那么我会得到预期的视图,见下图。UseDeveloperExceptionPageStartup.Configure

enter image description here

我的方法:StartupConfigure

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    var supportedCultures = new[] { new CultureInfo("es-ES") };

    app.UseRequestLocalization(new RequestLocalizationOptions
        {
            DefaultRequestCulture = new RequestCulture("en-AU"),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        });

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage(new DeveloperExceptionPageOptions() { SourceCodeLineCount = 10 });
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    // TEST
    // app.Run(context => { throw new Exception("error xxxx"); });   
   

    app.UseHttpsRedirection();
    app.UseStaticFiles();

    app.UseRouting();
    app.UseAuthorization();
    app.UseSession();

    app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                 name: "areas",
                 pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

    app.UseCors(builder =>
        {
            builder.AllowAnyOrigin()
                   .AllowAnyMethod()
                   .AllowAnyHeader();
        });
}

非常感谢

asp.net-mvc asp.net-core 错误处理

评论

0赞 Brando Zhang 7/19/2023
你的意思是不要显示“如果我在控制器中抛出异常,错误页面不会显示”?如果将异常抛出控制器内部,会发生什么情况?
0赞 Neal Rogers 7/19/2023
DeveloperExceptionPage 不显示/显示。
0赞 Denny 10/30/2023
你能帮我们@BrandoZhang吗?:)

答: 暂无答案