2012-10-26 39 views
2

我有一個錯誤頁面,其佈局在大多數情況下都能正常工作,但當控制器返回部分視圖時出現錯誤時,錯誤頁面及其佈局將放置在局部視圖中。我想這就是邏輯,但我想錯誤頁面被加載爲整頁。我如何在不更改所有錯誤處理的情況下完成該操作在部分視圖中加載MVC錯誤頁面

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

回答

2

你可以只使用一個try catch塊,並在catch返回視圖(),而不是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?

進入如何分別處理每種類型的錯誤。請記住,您正在basecontroller中處理您的異常,而不是global.asax文件。如果你能夠改變你的異常處理,那將是更好的方法。

+0

謝謝@ rbj325!我喜歡第一種方法,但ErrorPage不是視圖,因此不起作用。如果我返回一個像這樣的錯誤視圖instad,返回視圖(「ErrorTest」),它仍然會被加載到局部視圖中並弄亂佈局。我對MVC很陌生,所以我不太瞭解第二種方法。 – user1777259

+1

再次感謝!但錯誤頁面仍然在部分視圖中加載。我發現唯一的辦法是在catch塊中有這個javascript重定向,但我認爲這是一個難以解決問題的方法。返回JavaScript(String.Format(「document.location ='{0}'」,「/ SystemPages/ErrorPage」)) – user1777259

+0

你能發佈你的錯誤頁面代碼嗎?您可以將任何頁面轉換爲視圖。您可能需要覆蓋佈局頁面。 – rbj325

相關問題