Question

In my Asp.Net MVC website, I'm trying to get the custom error page working. In the development machine, everything works. But when I publish the website to the production, it only works if I set the status code to 200 (or don't set it). Here's my web.config:

<customErrors mode="On" defaultRedirect="~/Error" redirectMode="ResponseRedirect">
  <error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>

And this is the ErrorController

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View("Error");
    }
    public ViewResult NotFound(string aspxerrorpath)
    {
        Response.StatusCode = 404; //if I comment out this line, it works
        return View("NotFound");
    }
}

If I comment out the code Response.StatusCode = 404; the error page shows up. Otherwise it shows the default 404 page on the website. Again, this works on my development machine. Am I missing something? How can I render the custom error page AND return the 404 code at the same time?

Was it helpful?

Solution

I had the same problem, but if you are using IIS 7 or greater you can use httpErrors in the web.config.:

<system.webServer>
    <httpErrors errorMode="Custom">
      <remove statusCode="404"/>
      <error statusCode="404" path="~/Error/NotFound" responseMode="ExecuteURL"/>
    </httpErrors>
</system.webServer>

Read more about it here: What is the difference between customErrors and httpErrors?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top