Skip to content

Instantly share code, notes, and snippets.

@Dkowald
Last active October 25, 2018 08:36
Show Gist options
  • Save Dkowald/6414276f4e3745473c699c1941549710 to your computer and use it in GitHub Desktop.
Save Dkowald/6414276f4e3745473c699c1941549710 to your computer and use it in GitHub Desktop.
MVC Exception based Http Error
using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
namespace kwd.gist
{
public class HttpError : Exception
{
public HttpError(IActionResult result)
{
ErrorResult = result;
}
public IActionResult ErrorResult { get; private set; }
}
public class HttpErrorFilter : ExceptionFilterAttribute
{
public override void OnException(ExceptionContext context)
{
if (context.Exception is HttpError httpError)
{
context.Result = httpError.ErrorResult;
context.ExceptionHandled = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment