Permalink
Browse files

Add Global Exception Handling

  • Loading branch information...
scottsauber committed Apr 4, 2017
1 parent 85e43d2 commit e972ecc7bd75bcc971f679947614e683f15d01e4
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Mvc;

namespace GlobalExceptionHandling.Controllers
@@ -10,6 +8,7 @@ public class HomeController : Controller
{
public IActionResult Index()
{
throw new Exception("Something bad happened.");
return View();
}

@@ -29,6 +28,22 @@ public IActionResult Contact()

public IActionResult Error()
{
var exceptionFeature = HttpContext.Features.Get<IExceptionHandlerPathFeature>();

if (exceptionFeature != null)
{
string routeWhereExceptionOccurred = exceptionFeature.Path;

Exception exceptionThatOccurred = exceptionFeature.Error;


//throw exceptionThatOccurred;
// TODO: Do something with the exception
// Log it with Serilog?
// Send an e-mail, text, fax, or carrier pidgeon? Maybe all of the above?
// Whatever you do, be careful to catch any exceptions, otherwise you'll end up in an endless loop of throwing exceptions.
}

return View();
}
}
@@ -44,7 +44,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerF
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseExceptionHandler("/Home/Error"); // Adding comment so this sticks out in the GitHub commit. Make sure you have this line.
}

app.UseStaticFiles();

0 comments on commit e972ecc

Please sign in to comment.