12

In my web.config, I have:

<system.web>
    <customErrors mode="On" defaultRedirect="Error.cshtml" />
</system.web>

In Views/Shared/Error.cshtml, I have:

@model System.Web.Mvc.HandleErrorInfo
@{
    ViewBag.Title = "Error";
}
<h2>
    Sorry, an error occurred while processing your request.
</h2>

If I put an invalid URL/route into my browser, I get this:

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: /Error.cshtml

Why can't Error.cshtml be found? The file is definitely in Views/Shared.

Community
  • 1
  • 1
devuxer
  • 41,681
  • 47
  • 180
  • 292
  • 1
    Possible duplicate of [How to make custom error pages work in ASP.NET MVC 4](http://stackoverflow.com/questions/13905164/how-to-make-custom-error-pages-work-in-asp-net-mvc-4) – Liam Oct 10 '16 at 14:04
  • Answer is applicable to MVC3 as well as 4 ^ – Liam Oct 10 '16 at 14:04

5 Answers5

9

The defaultRedirect won't go directly to a view. Your defaultRedirect looks like a razor view file which it can't process. For example: Where does it get the model from? It isn't, and can't, be specified in the config file so it can't process a view.

If you want more dynamic error pages in MVC you might want to read custom error pages and error handling in MVC 3

Colin Mackay
  • 18,736
  • 7
  • 61
  • 88
  • Your answer contradicts this answer: http://stackoverflow.com/a/13905859/507339 . From my testing, I actually experience that a model of type HandleErrorInfo indeed is injected by the framework. You might need to drop the ".cshtml" extension in web.config though – Nilzor Jan 03 '14 at 09:23
  • @Colin - If a global error handler is registered in FilterConfig (eg filters.Add(new HandleErrorAttribute());) there should be no problem. There is not a 1:1 requirement for a controller to exist for a view. It is just this way by convention. – Matthew Sep 21 '15 at 19:05
6

It works for me. Just make this change in web.config:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml">
    </customErrors>
</system.web>
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Rajesh
  • 99
  • 1
  • 1
  • 1
    This almost worked for me. I had to drop the .cshtml though: defaultRedirect="~/Public/Error" is what I needed. – Dronz Nov 17 '15 at 21:46
3

Edit you web.config as:

<system.web>
    <customErrors mode="On" defaultRedirect="/Home/Error" />
</system.web>

If you use asp.net mvc 1.0 or 2.0, you should add HandleError attribute for each controller:

[HandleError]
public class HomeController: Controller
{
    public ActionResult Error()
    {
        return View();
    }
}

if you use asp.net mvc 3.0:

public class MvcApplication : System.Web.HttpApplication
{
        protected void Application_Start()
        {
            GlobalFilters.Filters.Add(new HandleErrorAttribute());
        }
}
hellangle
  • 425
  • 3
  • 7
2

You should mark your controller action with [HandleError] attribute or register it as a global filter

EDIT: It happens because of custom errors' redirects functionality belongs to ASP.NET infrastructure and knows nothing about ASP.NET MVC.

You should create separate controller for managing redirects and specify url to it in your Web.config. Or you could create static html file and put it into root directory (or wherever else except Views directory)

xelibrion
  • 2,220
  • 1
  • 19
  • 24
  • Makes no difference...it still says it can't find Error.cshtml. And what if the user enters a URL that doesn't even contain a valid controller? – devuxer Jun 02 '11 at 08:03
  • Ah, now I get it. You should create separate controller for managing redirects and specify url to it in your Web.config. Or you should create static html file and don't put it into Views folder – xelibrion Jun 02 '11 at 08:35
1

One solution is create ErrorController

public class ErrorController : Controller
{
    //
    // GET: /Error/

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

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

}

Change Web.config

Liam
  • 27,717
  • 28
  • 128
  • 190
dotnetstep
  • 17,065
  • 5
  • 54
  • 72