I have asp.net core application. The implementation of configure method redirects the user to "Error" page when there is an exception ( in non Development environment)
However it only works if the exception occurs inside controller. If exception occurs outside of controller, for example in my custom middleware, then the user does not get redirected to error page.
How do i redirect user to "Error" page if there is an exception in the middleware.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseApplicationInsightsRequestTelemetry();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseApplicationInsightsExceptionTelemetry();
app.UseStaticFiles();
app.UseSession();
app.UseMyMiddleware();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
Update 1
I updated code above with the following two lines that were missing in initial post.
app.UseSession();
app.UseMyMiddleware();
Also I found why app.UseExceptionHandler
was not able to redirect to Error page.
When there is an exception in my middleware code, app.UseExceptionHandler("\Home\Error")
is redirecting to \Home\Error
as expected; but since that is a new request, my middleware was executing again and throwing exception again.
So to solve the issue i changed my middleware to execute only if context.Request.Path != "/Home/Error"
I am not sure if this is the correct way to solve this issue but its working.
public class MyMiddleWare
{
private readonly RequestDelegate _next;
private readonly IDomainService _domainService;
public MyMiddleWare(RequestDelegate next, IDomainService domain)
{
_next = next;
_domainService = domain;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Path != "/Home/Error")
{
if (context.User.Identity.IsAuthenticated && !context.Session.HasKey(SessionKeys.USERINFO))
{
// this method may throw exception if domain service is down
var userInfo = await _domainService.GetUserInformation(context.User.Name).ConfigureAwait(false);
context.Session.SetUserInfo(userInfo);
}
}
await _next(context);
}
}
public static class MyMiddleWareExtensions
{
public static IApplicationBuilder UseMyMiddleWare(this IApplicationBuilder builder)
{
return builder.UseMiddleware<MyMiddleWare>();
}
}
UseExeptionHandler
should be able to handle it, but your middleware needs to be registered after it. – Renaud