Custom error page not working in production

Viewed 1763

I have implemented custom 404 page in my ASP. NET MVC 5 application, at first I have tested it on my localhost, and it worked perfectly. But when I moved my settings to release config, and tried to test it in production, the custom 404 page was gone, there was default mvc error page. What did I do wrong?

Web.config

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
        <remove statusCode="404" />
        <error statusCode="404" responseMode="ExecuteURL" path="/Error/Index" />
    </httpErrors>
</system.webServer>

Controller

public class ErrorController : BaseController
{
    public ActionResult Index(string id)
    {
        ViewModel vm = new ViewModel();

        Response.StatusCode = 404;

        return View("~/Views/..../Index.cshtml", vm);
    }
}

enter image description here

3 Answers

I had faced the same issue. The code was working in Dev and not in Prod.

Just remove the line

Response.StatusCode = 404; //REMOVE

from the action method. It will work.

Related