0

I am trying to force my project to use custom made error page with /error address.

I did write ActionResult Error function in controller

    public ActionResult Error()
    {
        return View();
    }

made a simple View for Error

@{
    ViewBag.Title = "Error Page";
}

<h1>ERROR</h1>

added an error route to global

routes.MapRoute(
    "Error",
    "Error/",
    new { controller = "Home", action = "Error" }
);

and changed webconfig customErrors

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

yet when I type non-existing url I get

Server Error in '/' Application. The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Galery

Version Information: Microsoft .NET Framework Version:2.0.50727.1433; ASP.NET Version:2.0.50727.1433
------>

what am I doing wrong.

Well if I type localpath/Error I can get to Error path and it works fine. Why the redirection is not working?

I am using VS2012 and server started by it.

2 Answers 2

1

You'll also have to override the <system.webServer> error block to catch IIS level errors:

<configuration>
<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="PassThrough">
          <clear />
          <error statusCode="502" path="/Error" responseMode="File" />
          <error statusCode="501" path="/Error" responseMode="File" />
          <error statusCode="500" path="/Error" responseMode="File" />
          <error statusCode="412" path="/Error" responseMode="File" />
          <error statusCode="406" path="/Error" responseMode="File" />
          <error statusCode="405" path="/Error" responseMode="File" />
          <error statusCode="404" path="/Error" responseMode="File" />
          <error statusCode="403" path="/Error" responseMode="File" />
          <error statusCode="402" path="/Error" responseMode="File" />
          <error statusCode="401" path="/Error" responseMode="File" />
    </httpErrors>
</system.webServer>
</configuration>
Sign up to request clarification or add additional context in comments.

Comments

1

Errors and 404s are not the same thing. Your custom error thing will fire for unhandled exceptions, but a 404 is not considered to be an unhandled exception by the MVC framework. Create a "catch-all" route of "{*anything}" and route it to the error action method.

Also make sure you put this route as the VERY LAST ROUTE so the other routes aren't captured by it.

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.