Last active
October 25, 2018 08:36
-
-
Save Dkowald/6414276f4e3745473c699c1941549710 to your computer and use it in GitHub Desktop.
MVC Exception based Http Error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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