0
votes

In my web.config file I have customErrors on and default redirect to my controller ErrorController like so:

<customErrors mode="On" defaultRedirect="/Error/">
</customErrors>

What I am trying do now in my Index method of my ErrorController is get the error code (404, 405, etc), I have tried the following:

 public class ErrorController : Controller
{
    public ActionResult Index()
    {
        ViewBag.statusCode = System.Web.HttpContext.Current.Response.StatusCode;

        return View();
    }
}

But it always returns 200 because its redirecting to a page with the status code 200....When I goto a page that does not exists, I still get a 200 code instead of 404. I just want to have one page, get the error code instead of having multiple error pages. Is this possible?

1

1 Answers

1
votes

Remove the defaultRedirect and use the global.asax.cs

Enter some code like this.

[ValidateInput(false)]
        protected void Application_Error()
        {

             //prevent the errors coming back!
             this.Context.ClearError();

             //redirect
             this.Context.Response.Redirect($"~/Home/Error");

        }

The error code is probably in the this.Context somewhere use debug and try and find it.

Just an FYI this.Context.AllErrors has all the errors so can show you what went wrong.