微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在ASP.net Core 3.1中使用StackExchange.Exceptional.AspNetCore时如何显示友好错误页面

如何解决在ASP.net Core 3.1中使用StackExchange.Exceptional.AspNetCore时如何显示友好错误页面

我已经能够在我的asp.net core 3.1项目中成功使用StackExchange.Exceptional.AspNetCore进行sql错误记录和错误电子邮件报告。我已替换了下面显示的Startup.cs类中的认开发人员错误页面(ConfigureServices的第一行)方法。在配置方法中将其添加为中间件。

public class Startup
{
    public Startup(IConfiguration configuration,IWebHostEnvironment env)
    {
        Configuration = configuration;
        HostingEnvironment = env;
    }

    public IWebHostEnvironment HostingEnvironment { get; }
    public IConfiguration Configuration { get; }
    public ILifetimeScope AutofacContainer { get; private set; }

   
    public void ConfigureServices(IServiceCollection services)
    {

        services.AddExceptional(Configuration.GetSection("Exceptional"),settings =>
        {
            settings.Store.ApplicationName = "MyProject" + HostingEnvironment.EnvironmentName;
            settings.UseExceptionalPageOnThrow = HostingEnvironment.IsDevelopment();
        });

       services.AddDbContext<ApplicationDbContext>(options =>
            options.UseLazyLoadingProxies().UsesqlServer(
                Configuration.GetConnectionString("MyDb")));
       services.AddIdentity<ApplicationUser,ApplicationRole>(
            options =>
            {
                options.SignIn.RequireConfirmedAccount = false;
            })
        .AddEntityFrameworkStores<ApplicationDbContext>()
        .AddDefaultUI()
        .AddDefaultTokenProviders();

        services.ConfigureApplicationCookie(options =>
        {
            options.LoginPath = "/Login";
        });
        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromMinutes(30);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;
        });
        services.AddControllersWithViews();
        services.AddRazorPages().AddRazorRuntimeCompilation();
        services.AddExpressiveAnnotations();
        var appSettings = Configuration.GetSection("AppSettings");
        services.Configure<AppSettings>(appSettings);
        var pgSettings = Configuration.GetSection("PaymentGatewaySettings");
        services.Configure<PaymentGatewaySettings>(pgSettings);
        services.AddScoped<ValidateClientSubscriptionAttribute>();
        services.AddScoped<ValidateClientIssueAttribute>();
    }

    
    public void ConfigureContainer(ContainerBuilder builder)
    {
        var web = Assembly.GetExecutingAssembly();

        builder.RegisterassemblyTypes(web).AsImplementedInterfaces();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
       
        builder.RegisterassemblyTypes(AppDomain.CurrentDomain.GetAssemblies())
        .AsClosedTypesOf(typeof(IQueryHandler<,>));
    }

    public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
    {
        app.UseExceptional();

        this.AutofacContainer = app.applicationservices.GetAutofacRoot();

        if (!env.IsDevelopment())
        {
            //THIS DOES NOT WORK WITH EXCEPTIONAL
            //app.UseExceptionHandler("/Home/Error");
            
        }
        
        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSession();
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();

        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?}");

            endpoints.MapRazorPages();
        });

    }
}

我的要求很简单,当环境为Development时,我想显示StackExchange.Exceptional开发人员页面。在生产时,我想显示一些页面,并显示诸如“糟糕!!出现问题”链接返回页面。就是这样。

我似乎找不到任何对我有帮助的选择。 app.UseExceptionHandler(“ / Home / Error”)或app.UseStatusCodePagesWithReExecute之类的选项也不起作用。在生产环境中出现认的浏览器错误500页面

如果仍然有帮助,我正在使用Autofac for DI。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。