user1470994
user1470994

Reputation: 311

Custom Errors Work Locally Not Once Deployed On IIS

I have the following:

Web.config

<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
<customErrors mode="On" defaultRedirect="~/Error/ShowError">
  <error redirect="~/Error/ShowError/400" statusCode="400" />
  <error redirect="~/Error/ShowError/401" statusCode="401" />
  <error redirect="~/Error/ShowError/403" statusCode="403" />
  <error redirect="~/Error/ShowError/404" statusCode="404" />
</customErrors>
</system.web>

ErrorController

[AllowAnonymous]
public class ErrorController : Controller
{
    public ViewResult ShowError(int id)
    {
        Response.StatusCode = id;
        switch (id)
        {
            case 400:
                return View("~/Views/Error/400.cshtml");
            case 401:
                return View("~/Views/Error/401.cshtml");
            case 403:
                return View("~/Views/Error/403.cshtml");
            case 404:
                return View("~/Views/Error/404.cshtml");
            default:
                return View("~/Views/Error/404.cshtml");
        }
    }
}

FilterConfig

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        //filters.Add(new HandleErrorAttribute());
    }

Locally everything works fine, I get a custom error page and am happy, however as soon as I deploy the site to my web server, I no longer get my custom error messages and only the generic:

enter image description here

Do I need to add anything specific for the IIS configuration in my online environment?

I've compared the local Web.config with the deployed Web.config and there isn't anything different (that I can see).

Upvotes: 3

Views: 1194

Answers (1)

Khanh TO
Khanh TO

Reputation: 48972

Your error is overwritten by IIS custom error.

Try:

Response.TrySkipIisCustomErrors = true

Upvotes: 5

Related Questions