0

Here is my congure method in Startup file :

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddLog4Net("log4net.config");
        //app.UseDeveloperExceptionPage();
        app.UseExceptionHandler("/error");
        app.UseMiddleware(typeof(Common.WebTools.MiddleWares.Log4netMiddleware));

        app.UseMiddleware<Common.WebTools.MiddleWares.RequestBodyLoggingMiddleware>();

        var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(options.Value);

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

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapControllerRoute("Empty","", new { controller = "Home", action = nameof(Controllers.HomeController.Index) });
            endpoints.MapControllerRoute("Error", "error", new { controller = "Error", action = nameof(Controllers.ErrorController.Index) });               
            endpoints.MapControllerRoute("TunnelEnter", "enter", new { controller = "Home", action= nameof(Controllers.HomeController.Enter) });
            endpoints.MapControllerRoute("TunnelCancel", "cancel", new { controller = "Home", action= nameof(Controllers.HomeController.Cancel) });
            endpoints.MapControllerRoute("TunnelSuccess",   "thankyou", new { controller = "Home", action = nameof(Controllers.HomeController.ThankYou) });

            endpoints.MapFallbackToController(nameof(Controllers.HomeController.Index), "Home");
        });

        // use middleware and launch server for Vue
        app.UseSpaStaticFiles(new StaticFileOptions {
            OnPrepareResponse = ctx =>
            {
                var headers = ctx.Context.Response.GetTypedHeaders();
                headers.CacheControl = new Microsoft.Net.Http.Headers.CacheControlHeaderValue { MaxAge = TimeSpan.FromDays(30*12) };
            }
        });
        app.UseSpa(spa =>
        {
            spa.Options.SourcePath = "client-app";
            if (env.IsDevelopment())
            {
                spa.UseProxyToSpaDevelopmentServer("http://localhost:9001");
            }
        });
    }

app.UseExceptionHandler("/error") doesn't raise anything, my "Index" method in "ErrorController" is never called. (If I call manually "/error" url, my custom page displays correctly.)

But if I use instead "app.UseDeveloperExceptionPage();", I have the system page with the error detailled.

What's wrong with my call ? "app.UseRouting()," is called after app.UseExceptionHandler as I saw here : Asp.Net 5/Core app.UseExceptionHandler() not working

UPDATE

After commenting line by line, I finally understand where the problem comes from. Here is my controller constructor

  public HomeController(IHttpContextAccessor httpContextAccessor)
  {
        // If I comment the following line, it works !!!!
        httpContextAccessor.HttpContext.Items.Add("a", "a");
  }

I If comment the line, no problem. But why it doesn't work if the line is not commented ???? What happens with the context

Any idea?

Rémy
  • 736
  • 1
  • 5
  • 19
  • Can you share your code with exception?I test with a 404 error,and it will go to Index action of ErrorController. – Yiyi You Mar 15 '22 at 08:35
  • @YiyiYou nothing more than [ResponseCache(NoStore = true, Duration = 0, Location = ResponseCacheLocation.None)] public async Task< IActionResult> Index(string id) { throw new Exception("test"); } – Rémy Mar 15 '22 at 14:14

0 Answers0