Ich habe eine sehr einfache asp.net MVC 6 App mit grundlegenden Routing, und ich habe eine benutzerdefinierte ErrorController, um Fehler zu bestimmten Ansichten zu routen.MVC 6 keine Ausnahme für undefinierte Routen
Ich erwarte jedoch, dass, wenn der Benutzer eine URL eingibt, die nicht existiert, eine Ausnahme ausgelöst wird (und ich kann damit umgehen). Es wird jedoch keine Ausnahme ausgelöst, wenn ich eine zufällige URL eintippe, ich erhalte nur eine leere Seite. Ich bin mir ziemlich sicher, dass dies in MVC 6.
ähnlich funktioniert Die Fehlerbehandlung selbst funktioniert gut, wenn ich nur eine Ausnahme in einem Controller auslösen.
Startup.cs (teilweise)
public void Configure(IApplicationEnvironment appEnv, IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(HandleException);
}
app.UseStaticFiles();
app.UseMvc(routes => MapRoutes(routes, appEnv));
}
private static void MapRoutes(IRouteBuilder routes, IApplicationEnvironment env)
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "main", action = "index" });
}
private static void HandleException(IApplicationBuilder errorApp)
{
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
errorApp.Run(async context => HandleErrorContext(context));
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
}
private static void HandleErrorContext(HttpContext context)
{
var error = context.Features.Get<IExceptionHandlerFeature>();
var exception = error.Error;
if (exception == null)
{
context.Response.Redirect("../error/external");
}
else if (exception is ExpirationException)
{
context.Response.Redirect("../error/expired");
}
else if (exception is HttpException)
{
var httpException = exception as HttpException;
int code = httpException.GetHttpCode();
context.Response.Redirect("../error/external?code=" + code);
}
else
{
context.Response.Redirect("../error/external");
}
}
Nicht zu sehen, wo Sie benutzerdefinierte 'ErrorController' in der Pipeline enthalten. Fehle ich etwas in deinem Code? – Nkosi
Ich habe eine andere Methode (unter was ich eingefügt habe), die das behandelt, was gut funktioniert, wenn ich eine Ausnahme manuell auslösen. Das Problem ist, dass überhaupt keine Ausnahme ausgelöst wird, so dass es nie zu diesem Punkt kommt. – user2713516