MVC错误页面在部分视图中加载

时间:2012-10-26 14:13:02

标签: asp.net-mvc error-handling partial-views

我有一个错误页面,其布局在大多数情况下都能正常工作,但是当控制器中出现错误并返回局部视图时,错误页面及其布局将放置在局部视图中。我想这是合乎逻辑的,但我希望将错误页面加载为整页。如何在不更改所有错误处理的情况下完成此操作。

的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>

1 个答案:

答案 0 :(得分:2)

你可以使用try catch块并在catch中返回一个View()而不是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);
}

这篇文章:ASP.NET MVC Custom Error Handling Application_Error Global.asax?

分别介绍了如何处理每种类型的错误。请记住,您正在使用basecontroller而不是global.asax文件处理异常。如果你能够改变你的异常处理,那将是更好的方法。