简体   繁体   中英

Using customError page with /error address

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.

Errors and 404 s are not the same thing. Your custom error thing will fire for unhandled exception s, 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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM