Hanz Husseiner
Hanz Husseiner

Reputation: 33

unable to display my custom error page on ASP.NET MVC5

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.

Upvotes: 0

Views: 47

Answers (1)

Yash Soni
Yash Soni

Reputation: 764

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();
    }
}

Upvotes: 1

Related Questions