2

My present configuration:

public void ConfigureDevelopment(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    //app.UseDeveloperExceptionPage();
    //app.UseDatabaseErrorPage();

    app.UseStatusCodePagesWithRedirects("/error/{0}");
    app.UseExceptionHandler();
    Configure(app);
}

I have this controller that is supposed to be executed when the server register any exceptions. When I execute "error/test" I'm redirected to "error/500" as expected. If I manually execute "error/exception", I get the server 500 native error, not mine.

public class ErrorController : Controller
{
    [Route("error/404")]
    public ActionResult Error404()
    {
        return View("404");
    }

    [Route("error/500")]
    public ActionResult Error500()
    {
        return View("500");
    }

    [Route("error/test")]
    public ActionResult Test()
    {
        return new StatusCodeResult(500);
    }

    [Route("error/exception")]
    public ActionResult Exception()
    {
        throw new Exception("Should redirect to error/500");
        return Content("nope");
    }
}

Any idea how to redirect "error/exception" to "error/500" when exception is thrown in actions?

Thank you.

1

2 Answers 2

1

Sure, no problem. use app.UseExceptionHandler("/error/500"); and then this isn't needed any longer:

[Route("error/exception")]
public ActionResult Exception()
{
    throw new Exception("Should redirect to error/500");
    return Content("nope");
}
Sign up to request clarification or add additional context in comments.

Comments

0

I believe you can specify multiple routes on one action so you dont need the "Exception" action, just add it's route to the 500:

public class ErrorController : Controller
{
    [Route("error/404")]
    public ActionResult Error404()
    {
        return View("404");
    }

    [Route("error/500")]
    [Route("error/exception")]
    public ActionResult Error500()
    {
        return View("500");
    }

    [Route("error/test")]
    public ActionResult Test()
    {
        return new StatusCodeResult(500);
    }

} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.