0

I am using asp.net MVC5 I created a custom error page under Views/shared/Error.cshtml I updated the web.config with the following;

<customErrors mode="On" defaultRedirect="~/Shared/Error.cshtml" />

I also created an error controller class under Controllers as follows;

`public class ErrorController : Controller
{
    // GET: Error
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Error()
    {
        return View();
    }
}`

when I run the application and put invalid URL I do not get my custom error page that i created, i still get the common system error page. what could be cause for not showing it? ... thank you.

Hanz Husseiner
  • 33
  • 1
  • 1
  • 6
  • Take a look at an answer I gave and see if it works for you https://stackoverflow.com/questions/39285429/how-to-handle-404-error-in-config-and-code-in-mvc5/39285920#39285920 – Nkosi Nov 22 '18 at 21:38

1 Answers1

1

you need to define your error page in web config

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error">
      <error redirect="~/Error/NotFound" statusCode="404" />
    </customErrors>
</system.web>

here's the controller

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult NotFound()
    {
        Response.StatusCode = 404;
        return View();
    }
}
Yash Soni
  • 764
  • 3
  • 14