Laden...

Ist bei ASP.NET Core 3 mit EndpointRouting IStatusCodeReExecuteFeature nicht mehr funktionsfähig?

Erstellt von Taipi88 vor 4 Jahren Letzter Beitrag vor 4 Jahren 1.555 Views
Taipi88 Themenstarter:in
1.029 Beiträge seit 2010
vor 4 Jahren
Ist bei ASP.NET Core 3 mit EndpointRouting IStatusCodeReExecuteFeature nicht mehr funktionsfähig?

Hi,

ich versuche gerade mich mit EndpointRouting auseinanderzusezten und habe leider Gottes ein Problem. Vom Prinzip hat das EndpointRouting ja sicher seine Vorteile - aber nach jeder Menge Tests muss ich feststellen: Das IStatusCodeReExecuteFeature ist nicht mehr funktionsfähig damit - sehe ich das richtig?

Mein Problem:
Verwende ich MVC kann ich problemlos mit Hilfe des vorgenannten Features einen HTTP404 samt trotzdem einer netten Seite aus einem Controller zurückgeben (und zwar unter Berücksichtigung des originalen Paths) - mit EndpointRouting wird das IStatusCodeReExecuteFeature schlicht gar nicht berücksichtigt bzw. ich kann ihn dazu nicht bewegen.

Als Beispiel:
(Dieser Code verwendet Redirects, womit ein (eigentlicher) 404 mit einem 302 und einem entsprechenden Redirect beantwortet wird - mies, denn eigentlich will ich ja den originalen Path und StatusCode)
--> Dieses Beispiel funktioniert problemlos - vorausgesetzt man hat einen entsprechenden StatusCodeController


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthorization();
            app.UseStatusCodePagesWithRedirects("/StatusCode/Index/{0}");
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
            });
        }

Beispiel 2:
(Dieser Code soll ein Reexecute ausführen - macht jedoch letztendlich rein gar nichts, womit ein 404 (von welcher Pipeline auch immer) mit dem schlichten StatusCode 404 - und sonst gar nichts) beantwortet wird)


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();
            app.UseRouting();
            app.UseAuthorization();
            app.UseStatusCodePagesWithReExecute("/StatusCode/Index/{0}");
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
            });
        }

Meine Fragen:
a) Mache ich was falsch?
b) Geht das überhaupt noch was ich beabsichtige?
c) Gibt es einen Umweg zum Ziel?

LG

16.835 Beiträge seit 2008
vor 4 Jahren

Es gab in UseStatusCodePagesWithReExecute in der ASP.NET Core 3.0 Preview eine Regression, weshalb die Pipe nicht funktioniert hat wie unter 2.0.
Das wurde aber schon beim Release von 3.0 behoben.

Wenn mich nicht alles täuscht, dann müsste

app.UseStatusCodePagesWithReExecute("/StatusCode/Index/{0}");

folgendes sein:

app.UseStatusCodePagesWithReExecute("/StatusCode", "?Code={0}");

Das mit "Index" sieht jedenfalls fischig aus.

Taipi88 Themenstarter:in
1.029 Beiträge seit 2010
vor 4 Jahren

Hi,

Vielen Dank für die Antwort.

Falls relevant - ich arbeite aktuell um genau zu sein mit .NetCore 3.1.

Das mit "Index" sieht jedenfalls fischig aus

Recht hast du - hab den Controller nun folgendermaßen korrigiert:

[Route("StatusCode/{id}")]
        public IActionResult Index(int id)
        {
            var reExecute = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
            if (reExecute != null)
            {
                _logger.LogInformation($"Status Code: {id}, OriginalPath: {reExecute.OriginalPath}");
            }

            return View(id);
        }

Allerdings - mein Problem bleibt - per Redirect erhalte ich meine View zurück - per Reexecute erhalte ich einen nackten 404. Ich habe testweise mal das Logging auf Debug gestellt - allerdings gibt das auch sehr wenig Aufschluss...

app.UseStatusCodePagesWithRedirects("/StatusCode/{0}");

führt zu:


Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 GET http://localhost:60422/Home/234  
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Debug: The request path /Home/234 does not match a supported file type
Microsoft.AspNetCore.Routing.Matching.DfaMatcher: Debug: No candidates found for the request path '/Home/234'
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware: Debug: Request did not match any endpoints
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 17.2046ms 302 
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 GET http://localhost:60422/StatusCode/404  

Hab den Controller für Reexecute mal angepasst:


public IActionResult Code(int code)
        {
            var reExecute = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
            if (reExecute != null)
            {
                _logger.LogInformation($"Status Code: {code}, OriginalPath: {reExecute.OriginalPath}");
            }

            return View("Index", code);
        }

app.UseStatusCodePagesWithReExecute("/StatusCode/Code", "?code={0}");

führt zu:


Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 GET http://localhost:60422/x324  
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware: Debug: The request path /x324 does not match a supported file type
Microsoft.AspNetCore.Routing.Matching.DfaMatcher: Debug: No candidates found for the request path '/x324'
Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware: Debug: Request did not match any endpoints
Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request finished in 63.9491ms 404 

Weitere Einfälle?

LG