3

I want to add custom error pages to my project. I found this post about my problem and i try to implement it.

So :

  • i add 404.cshtml, 404.html, 500.cshtml and 500.html pages
  • set response status code in added cshtml files
  • comment adding HandleErrorAttribute to global filters
  • update my web.config file

But now when i try to go by path http://localhost:120/foo/bar where my app is on http://localhost:120 i get next page :

Server Error in '/' Application.

Runtime Error

Description: An exception occurred while processing your request. Additionally, another exception occurred while executing the custom error page for the first exception. The request has been terminated.

I set <customErrors mode="Off" to see what problem is. It was - The resource cannot be found. which is logical. But when i set <customErrors mode="On" - i again get Runtime error.

What can cause it and how to solve it?


My config file :

<system.web>
    <customErrors mode="Off" redirectMode="ResponseRewrite" defaultRedirect="~/500.cshtml">
        <error statusCode="404" redirect="~/404.cshtml"/>
        <error statusCode="500" redirect="~/500.cshtml"/>
    </customErrors>
</system.web>

<system.webServer>
    <httpErrors errorMode="Custom">
        <remove statusCode="404"/>
        <error statusCode="404" path="404.html" responseMode="File"/>
        <remove statusCode="500"/>
        <error statusCode="500" path="500.html" responseMode="File"/>
    </httpErrors>
</system.webServer>

IIS version 8.5

demo
  • 6,038
  • 19
  • 75
  • 149
  • The paths in your `httpErrors` section look suspicious. Why is 404 prefixed with a / and not 500? Why do the cshtml files have the root path symbol and the html files do not? Do you have these in different directories? – Ron Beyer Aug 20 '15 at 12:09
  • I suspect the problem is that you are trying to use `.cshtml` erro page files. These are mvc views, they are not supposed to be called like that. Note that in original article author has created .aspx pages, because OP wanted to handled errors not by MVC but by ASP.NET pipeline. If you do want to handle this in MVC, your redirects should be to something like `/Error/Index`, to an action, not to a view directly – Andrei Aug 20 '15 at 12:11
  • @RonBeyer, yes, sorry. In `httpErrors ` it's my typo. And about `customErrors` - this is according to post. About directories - no all this pages are in Shared folder – demo Aug 20 '15 at 12:12
  • @Andrei, i also try with `.aspx` pages but error still the same : `Runtime Error` – demo Aug 20 '15 at 12:21
  • Possible duplicate of [Custom ASP.NET MVC 404 Error Page](http://stackoverflow.com/questions/553922/custom-asp-net-mvc-404-error-page) – Michael Freidgeim Jan 10 '17 at 12:57

3 Answers3

12

What version of IIS are you using? If 7+ then ignore custom errors using <customErrors mode="Off"> and use <httpErrors>. Using the method below the latter will show your error page without changing the URL which IMO is the preferred way of handling these things.

Set up an Error Controller and put your 404 and 500 in there like so:

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" />
    <remove statusCode="500" />
    <error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
    <error statusCode="500" path="/error/servererror" responseMode="ExecuteURL" />
 </httpErrors>

In the controller:

public class ErrorController : Controller
{
    public ActionResult servererror()
    {
        Response.TrySkipIisCustomErrors = true;
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        return View();
    }

    public ActionResult notfound()
    {
        Response.TrySkipIisCustomErrors = true;
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        return View();
    }

}

Then obviously set up the corresponding view for each error covered.

scgough
  • 5,099
  • 3
  • 30
  • 48
  • this example works for all cases that i tried to test except one. When i type `/foo/bar` i get standart message `Bad Request` instead of my custom page – demo Aug 20 '15 at 13:22
  • And if i add `defaultPath` and `defaultResponseMode` to `httpErrors` this will show me IIS page with 500 error – demo Aug 20 '15 at 13:25
  • Hmmm...you would possibly need to `[AllowHtml]` on the action in the controller or write custom code to globally catch and deliver the correct error – scgough Aug 20 '15 at 13:26
  • Locally you may still see the default IIS pages for errors but the production server should deliver your custom pages. – scgough Aug 20 '15 at 13:27
  • This was foiling me for hours, thanks! Here's the part that saved the day for me: `Response.TrySkipIisCustomErrors = true;` – Timothy Kanski Dec 08 '16 at 21:38
0

Your redirect url is not correct. Make it ~/404.html and ~/500.html. Notice that I changed the extension from .cshtml. Make sure the files exist and you are pointing to the correct location.

robasta
  • 4,621
  • 5
  • 35
  • 53
0

I had to do the following:

  1. Remove the defaultPath from httpErrors and defaultRedirect from customErrors.
  2. Make sure in Global.asax I had registered a default route:

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

I'm not sure why step 1 was necessary and step 2 was because we're doing EPiServer.

Syscall
  • 19,327
  • 10
  • 37
  • 52
Peheje
  • 12,542
  • 1
  • 21
  • 30