1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Fixed error handling page not using correct routes when generating URLs

This commit is contained in:
Uncled1023 2018-06-17 23:18:28 -07:00
parent 0a9ba75dcb
commit 2691501750
2 changed files with 15 additions and 4 deletions

View File

@ -2,7 +2,9 @@
using Microsoft.AspNetCore.Diagnostics; using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Newtonsoft.Json; using Newtonsoft.Json;
@ -23,10 +25,12 @@ namespace Teknik.Middleware
public class ErrorHandlerMiddleware public class ErrorHandlerMiddleware
{ {
private readonly RequestDelegate _next; private readonly RequestDelegate _next;
private readonly IRouter _router;
public ErrorHandlerMiddleware(RequestDelegate next) public ErrorHandlerMiddleware(RequestDelegate next, IRouter router)
{ {
_next = next; _next = next;
_router = router;
} }
public async Task Invoke(HttpContext httpContext, ILogger<Logger> logger, Config config, TeknikEntities dbContext) 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 routeData = new RouteData();
routeData.DataTokens.Add("area", "Error"); routeData.DataTokens.Add("area", "Error");
routeData.Values.Add("controller", "Error"); routeData.Values.Add("controller", "Error");
routeData.Routers.Add(_router);
var context = new ControllerContext(); var context = new ControllerContext();
context.HttpContext = httpContext; context.HttpContext = httpContext;
@ -90,9 +95,15 @@ namespace Teknik.Middleware
// Extension method used to add the middleware to the HTTP request pipeline. // Extension method used to add the middleware to the HTTP request pipeline.
public static class SetupErrorHandlerMiddlewareExtensions 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());
} }
} }
} }

View File

@ -134,7 +134,7 @@ namespace Teknik
}); });
// Use Exception Handling // Use Exception Handling
app.UseErrorHandler(); app.UseErrorHandler(config);
if (env.IsDevelopment()) if (env.IsDevelopment())
{ {