Search code examples
c#exceptionerror-handlingasp.net-mvc-5asp.net-mvc-views

MVC 5 Error page not showing


I am coding a MVC 5 internet application and am having a problem with errors.

When entering in some invalid View data, I am getting an error related to the error page being shown.

I am entering the following:

<html>test</html>

In my Global.asax file, I have the following:

protected void Application_Error(object sender, EventArgs e)

This is the error in the Application_Error function:

Message = "The controller for path '/Error/' was not found or does not implement IController."

In my Shared views folder, I have an error page called Error.cshtml.

Here is the code for the Error.cshtml:

@model System.Web.Mvc.HandleErrorInfo

@{
    if (TempData["UITitle"] != null)
    {
        ViewBag.Title = @TempData["UITitle"];
    }
    else
    {
        ViewBag.Title = "Error";    
    }
}

<h2 class="text-danger">
    @if (TempData["UIHeading"] != null)
    {
        @TempData["UIHeading"];
    }
    else
    {
        <p>Error</p>
    }
</h2>

<p>
    @if (TempData["UIMessage"] != null)
    {
        @TempData["UIMessage"];
    }
    else
    {
        if (Model != null)
        {
            @Model.Exception.Message
        }
        else
        {
            <p>Error</p>
        }
    }
</p>

Why am I getting the error:

Message = "The controller for path '/Error/' was not found or does not implement IController."

Thanks in advance.

Edit

I have added the following ActionResult in a controller:

public async Task<ActionResult> TestError()
{
    return View("Error");
}

The error page is shown correctly.

Also, I have ELMAH installed with email logging of exceptions.

EDIT2

I think the problem is in my web.config.

I have the following in the web.config:

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

Whenever an error occurs, I am wanting the Error.cshtml to be displayed that is in the Shared folder. I am guessing that the Error.cshtml cannot be found as it is in the Shared folder.

How can I achieve this?


Solution

  • Your error message suggests that you need an action method that corresponds to the URL => '/Error'.

    This action should then return View("Error.cshtml"); (with the correct path).

    If your redirection is in web.config file then try the following:

    <customErrors mode="On" defaultRedirect="~/Views/Shared/Error.cshtml">
    </customErrors>