0
votes

I am handling HttpError 404 and 500 in web.config using httpErrors module. Web.Config:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="404" path="/Error/Error" responseMode="Redirect"/>
  <error statusCode="500" path="/Error/Error" responseMode="Redirect"/>
</httpErrors>

Handle the error in Global.asax and perform operation based on 404 and 500.

    protected void Application_EndRequest()
    {
        if (Context.Response.StatusCode == 404 || Context.Response.StatusCode == 500)
        {
            Response.Clear();

            var rd = new RouteData();
            rd.Values["controller"] = "Error";
            rd.Values["action"] = "Error";

            IController controller = new ErrorController();
            controller.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
        }
    }

The code is working fine in Local and Test server but breaks in Production server. Testing method: write RandomText on the URL to alter route

Https://***.com/RandomText

In Local and Test server, system is redirected to custom error page via Error/Error route. But in Production server, system is giving blank page on entering above url. System should give be redirected to custom error page. Under network tab in chrome, i can see the status code returned is 403.

Status Code:403 Forbidden

I tried different approached such as including subStatusCode = -1 in under node and changing responseMode as ExecuteURL but nothing works.

Please let me know if anyone has any idea.

Thanks!!

1

1 Answers

0
votes

URLScan works differently. I resolved the issue by having a custom error page in my solution. Redirecting the use to controller action method will not work. Error page should be static page. Make it a HTML Or aspx. Web.config code:

 <httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="404" path="/Content/Error.html" responseMode="Redirect"/>
  <error statusCode="500" path="/Content/Error.html" responseMode="Redirect"/>
</httpErrors>

Happy Coding!!