1

I've created custom errors for my ASP.NET MVC application and for some reason they only show up on my local machine, but when I deploy my application on remote server the default IIS error pages show up (404 and 500) and I would like to understand why it is so. Here is my app configuration:

web.config

<system.web>
    ...
        <customErrors mode="On" defaultRedirect="~/ServerError">
          <error statusCode="404" redirect="~/NotFound" />
        </customErrors>
    ...
</system.web>

Routes configuration

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Error - 404",
        "NotFound",
        new { controller = "Error", action = "NotFound" }
    );

    routes.MapRoute(
        "Error - 500",
        "ServerError",
        new { controller = "Error", action = "ServerError" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

Error controller:

public class ErrorController : Controller
{
    [AllowAnonymous]
    public ActionResult ServerError()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;

        return View();
    }

    [AllowAnonymous]
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        return View();
    }
}

Global.asax

protected void Application_Error(object sender, EventArgs e)
{
    Response.TrySkipIisCustomErrors = true;
}

Local machine IIS configuration and remote one look similar, but maybe I've missed something and I have to configure it properly as well.

Thanks for any help.

1 Answer 1

1

This solves the problem:

public class ErrorController : Controller
{
    [AllowAnonymous]
    public ActionResult ServerError()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;

        Response.TrySkipIisCustomErrors = true;

        return View();
    }

    [AllowAnonymous]
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        Response.TrySkipIisCustomErrors = true;

        return View();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.