3

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

zdarsky.peter
  • 6,188
  • 9
  • 39
  • 60
  • Which version of IIS are you using? – SamGhatak Mar 11 '16 at 08:13
  • I am running Windows Server 2012 R2, so it should be IIS 8.5 – zdarsky.peter Mar 11 '16 at 08:26
  • I am looking into this thing, meanwhile I found [this](http://stackoverflow.com/questions/6648007/iis7-custom-404-not-showing) link, can you try this out? – SamGhatak Mar 11 '16 at 08:36
  • I have tried it, only thing that works is removing the httpErrors from transofrmation and keep it in base web.cofing file. I do not know why this is working, I have compared the transformated file with version that has 404 handling in base web.config and they are the same. – zdarsky.peter Mar 11 '16 at 08:52
  • Which web server are you using on your localhost? Cassini, IISExpress or IIS? And which version? In my experience, IIS httpErrors `ExecuteURL` mode is unable to go through MVC routing, causing the result you have on your server. But maybe have you some specific configuration (module execution order?) on your localhost allowing to solve this trouble. It could be very interesting for me if you could identify it. – Frédéric Mar 11 '16 at 10:36
  • Maybe should you check if the infamous [rammfar (`runAllManagedModulesForAllRequests`)](http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html) is active on your localhost. But avoid using that for "solving" the trouble, if it does. – Frédéric Mar 11 '16 at 11:03

3 Answers3

2

I remember it has to be set under the <system.web>. This is how it should be.

 <customErrors mode="Off" defaultRedirect="/Error/Index">
  <error statusCode="404" redirect="/Error/Index" />
</customErrors>

So setting the mode="Off" and mode="On" will toggle the Custom Error page on and off.

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59
  • 2
    `customErrors` and `httpErrors` are two different beasts. [`customErrors`](https://msdn.microsoft.com/en-us/library/h0hfz6fc%28v=vs.100%29.aspx) handles .Net failures error pages, while [`httpErrors`](https://www.iis.net/configreference/system.webserver/httperrors) applies to IIS default error pages. `customErrors` does not cover cases which does not involve a .Net pipeline, like requests on static resources. – Frédéric Mar 11 '16 at 10:42
  • @Frédéric EXCELENT!!! As a wrong page I was testing `Default.aspx1` that did not involve a .Net pipeline. Your comment was the only thing that could save my life!! – Farzin Kanzi Oct 30 '19 at 21:32
0

Try adding this to config file

<customErrors mode ="on" defaultRedirect="">

</customErrors>
It's a trap
  • 1,333
  • 17
  • 39
0

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.

Shemeemsha R A
  • 1,416
  • 16
  • 22