Browse Source

Fixed error handling page not using correct routes when generating URLs

tags/3.0.0^2
Teknikode 2 years ago
parent
commit
2691501750
2 changed files with 15 additions and 4 deletions
  1. 14
    3
      Teknik/Middleware/ErrorHandlerMiddleware.cs
  2. 1
    1
      Teknik/Startup.cs

+ 14
- 3
Teknik/Middleware/ErrorHandlerMiddleware.cs View File

@@ -2,7 +2,9 @@
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
@@ -23,10 +25,12 @@ namespace Teknik.Middleware
public class ErrorHandlerMiddleware
{
private readonly RequestDelegate _next;
private readonly IRouter _router;

public ErrorHandlerMiddleware(RequestDelegate next)
public ErrorHandlerMiddleware(RequestDelegate next, IRouter router)
{
_next = next;
_router = router;
}

public async Task Invoke(HttpContext httpContext, ILogger<Logger> logger, Config config, TeknikEntities dbContext)
@@ -66,6 +70,7 @@ namespace Teknik.Middleware
RouteData routeData = new RouteData();
routeData.DataTokens.Add("area", "Error");
routeData.Values.Add("controller", "Error");
routeData.Routers.Add(_router);

var context = new ControllerContext();
context.HttpContext = httpContext;
@@ -90,9 +95,15 @@ namespace Teknik.Middleware
// Extension method used to add the middleware to the HTTP request pipeline.
public static class SetupErrorHandlerMiddlewareExtensions
{
public static IApplicationBuilder UseErrorHandler(this IApplicationBuilder builder)
public static IApplicationBuilder UseErrorHandler(this IApplicationBuilder builder, Config config)
{
return builder.UseMiddleware<ErrorHandlerMiddleware>();
var routes = new RouteBuilder(builder)
{
DefaultHandler = builder.ApplicationServices.GetRequiredService<MvcRouteHandler>(),
};
routes.BuildRoutes(config);

return builder.UseMiddleware<ErrorHandlerMiddleware>(routes.Build());
}
}
}

+ 1
- 1
Teknik/Startup.cs View File

@@ -134,7 +134,7 @@ namespace Teknik
});

// Use Exception Handling
app.UseErrorHandler();
app.UseErrorHandler(config);

if (env.IsDevelopment())
{

Loading…
Cancel
Save