2

I have a error page with layout that works fine in most cases but when there is an error in a controller that returns a partial view the error page and its layout is placed in the partial view. I guess thats logical but I want the error page to be loaded as full page. How do I accomplish that without changing all error handling.

web.config:

<customErrors mode="On" defaultRedirect="~/Error">
  <error statusCode="500" redirect="~/SystemPages/ErrorPage" />
  <error statusCode="403" redirect="~/SystemPages/FileNotFound" />
  <error statusCode="404" redirect="~/SystemPages/FileNotFound" />
</customErrors>

Global.asax:

Shared Sub RegisterGlobalFilters(ByVal filters As GlobalFilterCollection)
    filters.Add(New HandleErrorAttribute())
End Sub

BaseController:

Protected Overrides Sub OnException(ByVal filterContext As ExceptionContext)

    If filterContext Is Nothing Then Return

    If TypeOf (filterContext.Exception) Is FaultException Then
        Dim CodeName As String = 
        CType(filterContext.Exception, FaultException).Code.Name
        Dim Message As String = CType(filterContext.Exception, FaultException).Message
        TempData("ErrorMessage") = Message
    Else
        Logging.LogDebugData(HamtaDebugInformation(filterContext.RouteData))
        Logging.WriteExceptionLog(filterContext.Exception)
        TempData("ErrorMessage") = filterContext.Exception.Message
    End If

    Response.Redirect("/SystemPages/ErrorPage")

End Sub

SearchController:

   Function GetData() As ActionResult
   ...
   Return PartialView("_Tab", vmData)

ErrorPage:

@Code
ViewData("Title") = "ErrorPage"
Layout = "~/Views/Shared/_Layout.vbhtml"
End Code

<div id="mainContent" class="oneColumn">
<div class="panel">
    <span class="panelTLC"></span>
    <span class="panelTRC"></span>
    <div id="inputPanel" class="panelContent">
        <div class="modul">
            <div class="modulHead">
                <span class="TLC"></span>
                <span class="TRC"></span>
            </div>
            <div class="modulContent">
                <span class="TLC"></span><span class="TRC"></span>
                <p>@ViewBag.ErrorMessage</p>
                <p>@TempData("ErrorMessage")</p>
                <span class="BLC"></span>
                <span class="BRC"></span>
            </div>
        </div>
    </div>
    <span class="panelBLC"></span><span class="panelBRC"></span>
</div>
</div>
user1777259
  • 21
  • 1
  • 3

1 Answers1

2

You could just use a try catch block and in the catch return a View() instead of PartialView().

Function GetData() As ActionResult
Try
...
Return PartialView("_Tab", vmData)
Catch ex as Exception
//Handle exception here ( send to error log, etc)
Return View("~/SystemPages/ErrorPage")
End Try

OR

web.config:

<customErrors mode="On"/>

BaseController:

Protected Overrides Sub OnException(ByVal filterContext As ExceptionContext)

    If filterContext Is Nothing Then Return

    Dim Message As String

    If TypeOf (filterContext.Exception) Is FaultException Then
        Dim CodeName As String = 
        CType(filterContext.Exception, FaultException).Code.Name
        Message = CType(filterContext.Exception, FaultException).Message
    Else
        Logging.LogDebugData(HamtaDebugInformation(filterContext.RouteData))
        Logging.WriteExceptionLog(filterContext.Exception)
        Message = filterContext.Exception.Message
    End If

    Response.Redirect(String.Format("~/Error/HttpError/?message={1}", "HttpError", Message))

End Sub

ErrorController:

public class ErrorController : Controller
{
    // GET: /Error/HttpError
    public ActionResult HttpError(string message) {
       return View("ErrorTest", message);
}

This post: ASP.NET MVC Custom Error Handling Application_Error Global.asax?

goes into how to handle each type of error separately. Keep in mind you are handling your exceptions in basecontroller instead of the global.asax file. Which if you were able to change your exception handling, that would be the better way to do it.

Community
  • 1
  • 1
rbj325
  • 954
  • 8
  • 17
  • Thank you @rbj325! I like the first approach but ErrorPage isn't a view so that doesn't work. If I return a error view instad like this, Return View("ErrorTest"), it will still be loaded in the partial view and mess up the layout. I'm quite new to MVC so I don't really understand the second approach. – user1777259 Oct 27 '12 at 14:09
  • 1
    Thanks again! But the error page is still loaded in the partial view. The only way i have found that works is to have this javascript redirect in the catch block but I think it's a ugly way to solve the problem. Return JavaScript(String.Format("document.location = '{0}'", "/SystemPages/ErrorPage")) – user1777259 Oct 28 '12 at 13:09
  • Can you post your error page code? You can turn any page into a view. You may need to just override the layout page. – rbj325 Oct 29 '12 at 14:37
  • 1
    This does not resolve your custom error page from showing up in the partial. – Ed DeGagne Jun 20 '14 at 19:48