1

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?

Simon
  • 7,991
  • 21
  • 83
  • 163

2 Answers2

0

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>
h-rai
  • 3,636
  • 6
  • 52
  • 76
  • I am getting the following error: {"Path '/Views/Shared/Error.cshtml' was not found."} – Simon Mar 03 '15 at 05:46
  • @user3736648 is the Error.cshtml file in Views/Shared folder? – h-rai Mar 03 '15 at 05:51
  • Yes it is, I am guessing it is protected when it is in the shared folder. – Simon Mar 03 '15 at 05:52
  • You could try to follow one of the error handling procedures in the provided link http://prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc Even if your Error page loads, you won't have the model in it as the model is an MVC object and you are already out of MVC context. The article has a thorough explanation on it. – h-rai Mar 03 '15 at 06:04
0

Add the following in your Global.asax

P.S: Change the name of the contr4oller and action as per your declarations and other mods as per ur need

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
        Dim exception As Exception = Server.GetLastError()
        If exception Is Nothing Then
            Exit Sub
        End If
        Dim httpException As HttpException = TryCast(exception, HttpException)
        If httpException IsNot Nothing Then

            Dim routeData As New RouteData()
            routeData.Values.Add("controller", "Error")
            Select Case httpException.GetHttpCode()
                Case 403, 404
                    ' page not found
                    routeData.Values.Add("action", "errorpage404")
                Case Else
                    routeData.Values.Add("action", "errorpage40x)
            End Select
            If httpException.GetHttpCode() = 500 Then
                'TODO
                'Logging
            End If

            'routeData.Values.Add("error", exception)
            ' clear error on server
            Response.Clear()
            Server.ClearError()
            Dim errorController As IController = New ErrorController()
            errorController.Execute(New RequestContext(New HttpContextWrapper(Context), routeData))
        End If
    End Sub
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47